From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D9DD4C433F5 for ; Thu, 19 May 2022 17:24:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242546AbiESRYo (ORCPT ); Thu, 19 May 2022 13:24:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241281AbiESRYf (ORCPT ); Thu, 19 May 2022 13:24:35 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C91A3A30B8; Thu, 19 May 2022 10:24:32 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id nk9-20020a17090b194900b001df2fcdc165so9406007pjb.0; Thu, 19 May 2022 10:24:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=AOewAjqDP3lAAYJgfWcVj1UOu5lLUzToNZ6Wvc+KtSg=; b=o7PvoY82ItPKwYBbxMn3eeGtB+88hpIirgruNU/rWUoKpUEAqSN8AOJmIKDsce5JFi UTYPp4rb4zmYUdXjv4RmmVYJVy3S+kuvgWvFsmqZDGGQmzxL2LeuQDl3ahDP4FRdZA8j 900dw1oX1FkKmt20m515iWbKsmS8VgKJL2hZYp5lKEvchzH0Eewp5UZJBqtqPqVic5EH RF20B4iGdR38wQe+qn6Kbh+VMTzLJvHrydMTvN5QZY567vkCoDhNPdPix0kYs/jXAINc 5tV2zX4xFQC9V9Rum8IT42fvmOhz3r6B3xqzH6NifaMli1XqIQwj81SHZJLbzm4LIZsZ 1Xug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=AOewAjqDP3lAAYJgfWcVj1UOu5lLUzToNZ6Wvc+KtSg=; b=zRYyoCVR6aFnlshEQu2JH0lkVe157NRIjvhRI+frGFzi1npR0ucTRHpvn8kzbXqzby tD/a/VYc+4wtF5s/ZReQfJGMwOrE39a4dQ+foYEQ8Cpzv89JNkDURUu46D2BAij8giMP tXYfOXJrgNQ/UoH1cRuRGOyY35UC5irvpluXSpNSKMnZFsZCk/P9ODGDGvcPv5qqebkR NCCydMRed/qlNiu5XHy1KlseUrCHCwC0VKUTpaclAnR6czq8n4f1x47AB3m5oabO2aOv L8hHhXEtDY971jyyr2BMQk2fexbdZ9rf9Vt+8FLPBVlF/ABK4GzDWgWqZnwUVwO9M8PM Qw3Q== X-Gm-Message-State: AOAM5314CAbS2OgOB3mYMCB0GyhMPPHCn2oV37IBw9BK4AjTnyhdwl+G 94VHaWkUDJkJFcJBM6ND1U7Ej7Qt0oLu X-Google-Smtp-Source: ABdhPJwzE8ZdLVW+yuKBJt7vmP5Ln6NcdLktqI4MYHA6QA9b9e/mWT6r/+ml164G4C5gZpReHJkGcQ== X-Received: by 2002:a17:903:22cd:b0:161:be20:577 with SMTP id y13-20020a17090322cd00b00161be200577mr5941328plg.54.1652981071739; Thu, 19 May 2022 10:24:31 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.24.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:24:31 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 01/28] lib/printbuf: New data structure for printing strings Date: Thu, 19 May 2022 13:23:54 -0400 Message-Id: <20220519172421.162394-2-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This adds printbufs: a printbuf points to a char * buffer and knows the size of the output buffer as well as the current output position. Future patches will be adding more features to printbuf, but initially printbufs are targeted at refactoring and improving our existing code in lib/vsprintf.c - so this initial printbuf patch has the features required for that. Signed-off-by: Kent Overstreet Reviewed-by: Matthew Wilcox (Oracle) --- include/linux/printbuf.h | 94 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 include/linux/printbuf.h diff --git a/include/linux/printbuf.h b/include/linux/printbuf.h new file mode 100644 index 0000000000..40dc07040d --- /dev/null +++ b/include/linux/printbuf.h @@ -0,0 +1,94 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ +/* Copyright (C) 2022 Kent Overstreet */ + +#ifndef _LINUX_PRINTBUF_H +#define _LINUX_PRINTBUF_H + +#include + +/* + * Printbufs: String buffer for outputting (printing) to, for vsnprintf + */ + +struct printbuf { + char *buf; + unsigned size; + unsigned pos; +}; + +static inline unsigned printbuf_remaining(struct printbuf *out) +{ + return out->pos < out->size ? out->size - out->pos : 0; +} + +static inline unsigned printbuf_written(struct printbuf *out) +{ + return min(out->pos, out->size); +} + +static inline void printbuf_nul_terminate(struct printbuf *out) +{ + if (out->pos < out->size) + out->buf[out->pos] =3D 0; + else if (out->size) + out->buf[out->size - 1] =3D 0; +} + +static inline void pr_chars(struct printbuf *out, char c, unsigned n) +{ + memset(out->buf + out->pos, + c, + min(n, printbuf_remaining(out))); + out->pos +=3D n; + printbuf_nul_terminate(out); +} + +static inline void __pr_char(struct printbuf *out, char c) +{ + if (printbuf_remaining(out)) + out->buf[out->pos] =3D c; + out->pos++; +} + +static inline void pr_char(struct printbuf *out, char c) +{ + __pr_char(out, c); + printbuf_nul_terminate(out); +} + +static inline void pr_bytes(struct printbuf *out, const void *b, unsigned = n) +{ + memcpy(out->buf + out->pos, + b, + min(n, printbuf_remaining(out))); + out->pos +=3D n; + printbuf_nul_terminate(out); +} + +static inline void pr_str(struct printbuf *out, const char *str) +{ + pr_bytes(out, str, strlen(str)); +} + +static inline void pr_hex_byte(struct printbuf *out, u8 byte) +{ + __pr_char(out, hex_asc_hi(byte)); + __pr_char(out, hex_asc_lo(byte)); + printbuf_nul_terminate(out); +} + +static inline void pr_hex_byte_upper(struct printbuf *out, u8 byte) +{ + __pr_char(out, hex_asc_upper_hi(byte)); + __pr_char(out, hex_asc_upper_lo(byte)); + printbuf_nul_terminate(out); +} + +#define PRINTBUF ((struct printbuf) { .si_units =3D PRINTBUF_UNITS_10 }) +#define PRINTBUF_EXTERN(_buf, _size) \ +((struct printbuf) { \ + .buf =3D _buf, \ + .size =3D _size, \ +}) + +#endif /* _LINUX_PRINTBUF_H */ --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 811E2C433EF for ; Thu, 19 May 2022 17:25:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238246AbiESRZU (ORCPT ); Thu, 19 May 2022 13:25:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242585AbiESRYm (ORCPT ); Thu, 19 May 2022 13:24:42 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E652A30A6; Thu, 19 May 2022 10:24:37 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id t11-20020a17090a6a0b00b001df6f318a8bso9347050pjj.4; Thu, 19 May 2022 10:24:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=e+dNM37QEx17IBKysc6iUKUoZP5TiyCNd77IbXsvUTU=; b=ilYhHUCTeM2PGbu+Gbl82yFrFoxxHnlsrFbrmojIxAT4eb/TExD3ZCdh4zoRysxDqa UWzZgGTls4Xal+cjkq7JsA7H/aIr1gRMzr5mKPFuEp+8mjxJZdcGX8mJqsNkJhZQMUj+ RdgOd03pQrmMWyIrc3qKo6zicrjJH3wD/oRx4jFMu/nGl4jILdND6OlwQl5nK9swLtPA 0ya81KB505meC6JJJJRge7+TFTqBv/5S+ezDnQ8ypf1ovYLPd4TDRCnMhNH81pcibX93 6ZlxTBabn3PcaBiR+Asxe9QOOren+VnqTyD8+exbaxesWYH4uxsMsQRXkhe14RHjiShH 6IjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=e+dNM37QEx17IBKysc6iUKUoZP5TiyCNd77IbXsvUTU=; b=GsWdpTltEklf5DDNGTRr4OaUY55pRz1+3jdIOH/haKc2A+42eUKOyPcARJjGnwjkgH /ewT+BsInwsvxUBWWPzeyPbHmSFgsaPcv4AK8ZjUI71ADTlYt104b7ynzPcl30YjWfjZ Y944IjyvWPV8DMVRhptGkTwZ6IzE8ubZHFW4FvDeigAwm6NgSnsMuMEfKVJT7xh8u3h5 Y+rfBRlQ5m3lNWtl6vrM/WbfZ0tMK4sKQ/pSkEsh+a8oolq/1J+J3T65OtgNyFjXVyYP xecLZZied1+3m0pYl6w9tt8ihqaP1Tx4NrcHXd0oMNR6DSAXtheMAgXvc/houIAhbXds msVg== X-Gm-Message-State: AOAM531OfW1V82psw8BJlNQFQWh/RbFNlMD9I9MsrR+H3IbKT4WX9lVh HUjCNGEId6/vxILzlShmMdYuv4RfIp18 X-Google-Smtp-Source: ABdhPJxYOYGpapJgRGgJXVv/CJvK0rdMk+XsqzUeNJCcAV632qYmKqzc0xOkEOta+wFIUSXbXtBjKQ== X-Received: by 2002:a17:902:ef43:b0:156:9c5d:b0fe with SMTP id e3-20020a170902ef4300b001569c5db0femr5703136plx.158.1652981074766; Thu, 19 May 2022 10:24:34 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.24.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:24:33 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 02/28] vsprintf: Convert to printbuf Date: Thu, 19 May 2022 13:23:55 -0400 Message-Id: <20220519172421.162394-3-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This converts vsnprintf() to printbufs: instead of passing around raw char * pointers for current buf position and end of buf, we have a real type! This makes the calling convention for our existing pretty printers a lot saner and less error prone, plus printbufs add some new helpers that make the code smaller and more readable, with a lot less crazy pointer arithmetic. There are a lot more refactorings to be done: this patch tries to stick to just converting the calling conventions, as that needs to be done all at once in order to avoid introducing a ton of wrappers that will just be deleted. Thankfully we have good unit tests for printf, and they have been run and are all passing with this patch. We have two new exported functions with this patch: - pr_buf(), which is like snprintf but outputs to a printbuf - vpr_buf, like vsnprintf These are the actual core print routines now - vsnprintf() is a wrapper around vpr_buf(). Signed-off-by: Kent Overstreet --- include/linux/kernel.h | 4 + include/linux/string_helpers.h | 4 + lib/string_helpers.c | 11 + lib/vsprintf.c | 1261 ++++++++++++++------------------ 4 files changed, 576 insertions(+), 704 deletions(-) diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 33f47a9965..ca9149de7c 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -202,6 +202,10 @@ extern int num_to_str(char *buf, int size, =20 /* lib/printf utilities */ =20 +struct printbuf; +extern __printf(2, 3) void pr_buf(struct printbuf *out, const char *fmt, .= ..); +extern __printf(2, 0) void vpr_buf(struct printbuf *out, const char *fmt, = va_list); + extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...); extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list); extern __printf(3, 4) diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h index 7a22921c9d..2e56b4ce98 100644 --- a/include/linux/string_helpers.h +++ b/include/linux/string_helpers.h @@ -10,6 +10,7 @@ struct device; struct file; struct task_struct; +struct printbuf; =20 /* Descriptions of the types of units to * print in */ @@ -71,6 +72,9 @@ static inline int string_escape_mem_any_np(const char *sr= c, size_t isz, return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, only); } =20 +void pr_escaped_string(struct printbuf *out, const char *src, size_t isz, + unsigned int flags, const char *only); + static inline int string_escape_str(const char *src, char *dst, size_t sz, unsigned int flags, const char *only) { diff --git a/lib/string_helpers.c b/lib/string_helpers.c index 90f9f1b7af..c890e83f59 100644 --- a/lib/string_helpers.c +++ b/lib/string_helpers.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -587,6 +588,16 @@ int string_escape_mem(const char *src, size_t isz, cha= r *dst, size_t osz, } EXPORT_SYMBOL(string_escape_mem); =20 +void pr_escaped_string(struct printbuf *out, + const char *src, size_t isz, + unsigned int flags, const char *only) +{ + out->pos +=3D string_escape_mem(src, isz, out->buf + out->pos, + printbuf_remaining(out), + flags, only); +} +EXPORT_SYMBOL(pr_escaped_string); + /* * Return an allocated string that has been escaped of special characters * and double quotes, making it safe to log in quotes. diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 3b8129dd37..09b259e030 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -44,6 +44,7 @@ #ifdef CONFIG_BLOCK #include #endif +#include =20 #include "../mm/internal.h" /* For the trace_print_flags arrays */ =20 @@ -446,8 +447,8 @@ static_assert(sizeof(struct printf_spec) =3D=3D 8); #define PRECISION_MAX ((1 << 15) - 1) =20 static noinline_for_stack -char *number(char *buf, char *end, unsigned long long num, - struct printf_spec spec) +void number(struct printbuf *out, unsigned long long num, + struct printf_spec spec) { /* put_dec requires 2-byte alignment of the buffer. */ char tmp[3 * sizeof(num)] __aligned(2); @@ -507,67 +508,40 @@ char *number(char *buf, char *end, unsigned long long= num, if (i > precision) precision =3D i; /* leading space padding */ - field_width -=3D precision; + field_width =3D max(0, field_width - precision); if (!(spec.flags & (ZEROPAD | LEFT))) { - while (--field_width >=3D 0) { - if (buf < end) - *buf =3D ' '; - ++buf; - } + pr_chars(out, ' ', field_width); + field_width =3D 0; } /* sign */ - if (sign) { - if (buf < end) - *buf =3D sign; - ++buf; - } + if (sign) + __pr_char(out, sign); /* "0x" / "0" prefix */ if (need_pfx) { - if (spec.base =3D=3D 16 || !is_zero) { - if (buf < end) - *buf =3D '0'; - ++buf; - } - if (spec.base =3D=3D 16) { - if (buf < end) - *buf =3D ('X' | locase); - ++buf; - } + if (spec.base =3D=3D 16 || !is_zero) + __pr_char(out, '0'); + if (spec.base =3D=3D 16) + __pr_char(out, 'X' | locase); } /* zero or space padding */ if (!(spec.flags & LEFT)) { char c =3D ' ' + (spec.flags & ZEROPAD); =20 - while (--field_width >=3D 0) { - if (buf < end) - *buf =3D c; - ++buf; - } + pr_chars(out, c, field_width); + field_width =3D 0; } /* hmm even more zero padding? */ - while (i <=3D --precision) { - if (buf < end) - *buf =3D '0'; - ++buf; - } + if (precision > i) + pr_chars(out, '0', precision - i); /* actual digits of result */ - while (--i >=3D 0) { - if (buf < end) - *buf =3D tmp[i]; - ++buf; - } + while (--i >=3D 0) + __pr_char(out, tmp[i]); /* trailing space padding */ - while (--field_width >=3D 0) { - if (buf < end) - *buf =3D ' '; - ++buf; - } - - return buf; + pr_chars(out, ' ', field_width); } =20 static noinline_for_stack -char *special_hex_number(char *buf, char *end, unsigned long long num, int= size) +void special_hex_number(struct printbuf *out, unsigned long long num, int = size) { struct printf_spec spec; =20 @@ -577,25 +551,28 @@ char *special_hex_number(char *buf, char *end, unsign= ed long long num, int size) spec.base =3D 16; spec.precision =3D -1; =20 - return number(buf, end, num, spec); + number(out, num, spec); } =20 -static void move_right(char *buf, char *end, unsigned len, unsigned spaces) +/* + * inserts @spaces spaces @len from the end of @out + */ +static void move_right(struct printbuf *out, + unsigned len, unsigned spaces) { - size_t size; - if (buf >=3D end) /* nowhere to put anything */ - return; - size =3D end - buf; - if (size <=3D spaces) { - memset(buf, ' ', size); - return; - } - if (len) { - if (len > size - spaces) - len =3D size - spaces; - memmove(buf + spaces, buf, len); - } - memset(buf, ' ', spaces); + unsigned move_src =3D out->pos - len; + unsigned move_dst =3D move_src + spaces; + unsigned remaining_from_dst =3D move_dst < out->size ? out->size - move_d= st : 0; + unsigned remaining_from_src =3D move_src < out->size ? out->size - move_s= rc : 0; + + BUG_ON(len > out->pos); + + memmove(out->buf + move_dst, + out->buf + move_src, + min(remaining_from_dst, len)); + memset(out->buf + move_src, ' ', + min(remaining_from_src, spaces)); + out->pos +=3D spaces; } =20 /* @@ -607,67 +584,55 @@ static void move_right(char *buf, char *end, unsigned= len, unsigned spaces) * Returns: new buffer position after padding. */ static noinline_for_stack -char *widen_string(char *buf, int n, char *end, struct printf_spec spec) +void widen_string(struct printbuf *out, int n, + struct printf_spec spec) { unsigned spaces; =20 if (likely(n >=3D spec.field_width)) - return buf; + return; /* we want to pad the sucker */ spaces =3D spec.field_width - n; - if (!(spec.flags & LEFT)) { - move_right(buf - n, end, n, spaces); - return buf + spaces; - } - while (spaces--) { - if (buf < end) - *buf =3D ' '; - ++buf; - } - return buf; + if (!(spec.flags & LEFT)) + move_right(out, n, spaces); + else + pr_chars(out, ' ', spaces); } =20 /* Handle string from a well known address. */ -static char *string_nocheck(char *buf, char *end, const char *s, - struct printf_spec spec) +static void string_nocheck(struct printbuf *out, + const char *s, + struct printf_spec spec) { - int len =3D 0; - int lim =3D spec.precision; + int len =3D min_t(unsigned, strlen(s), spec.precision); =20 - while (lim--) { - char c =3D *s++; - if (!c) - break; - if (buf < end) - *buf =3D c; - ++buf; - ++len; - } - return widen_string(buf, len, end, spec); + pr_bytes(out, s, len); + widen_string(out, len, spec); } =20 -static char *err_ptr(char *buf, char *end, void *ptr, - struct printf_spec spec) +static void err_ptr(struct printbuf *out, void *ptr, + struct printf_spec spec) { int err =3D PTR_ERR(ptr); const char *sym =3D errname(err); =20 - if (sym) - return string_nocheck(buf, end, sym, spec); - - /* - * Somebody passed ERR_PTR(-1234) or some other non-existing - * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=3Dn. Fall back to - * printing it as its decimal representation. - */ - spec.flags |=3D SIGN; - spec.base =3D 10; - return number(buf, end, err, spec); + if (sym) { + string_nocheck(out, sym, spec); + } else { + /* + * Somebody passed ERR_PTR(-1234) or some other non-existing + * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=3Dn. Fall back to + * printing it as its decimal representation. + */ + spec.flags |=3D SIGN; + spec.base =3D 10; + number(out, err, spec); + } } =20 /* Be careful: error messages must fit into the given buffer. */ -static char *error_string(char *buf, char *end, const char *s, - struct printf_spec spec) +static void error_string(struct printbuf *out, const char *s, + struct printf_spec spec) { /* * Hard limit to avoid a completely insane messages. It actually @@ -677,7 +642,7 @@ static char *error_string(char *buf, char *end, const c= har *s, if (spec.precision =3D=3D -1) spec.precision =3D 2 * sizeof(void *); =20 - return string_nocheck(buf, end, s, spec); + string_nocheck(out, s, spec); } =20 /* @@ -696,14 +661,15 @@ static const char *check_pointer_msg(const void *ptr) return NULL; } =20 -static int check_pointer(char **buf, char *end, const void *ptr, +static int check_pointer(struct printbuf *out, + const void *ptr, struct printf_spec spec) { const char *err_msg; =20 err_msg =3D check_pointer_msg(ptr); if (err_msg) { - *buf =3D error_string(*buf, end, err_msg, spec); + error_string(out, err_msg, spec); return -EFAULT; } =20 @@ -711,18 +677,19 @@ static int check_pointer(char **buf, char *end, const= void *ptr, } =20 static noinline_for_stack -char *string(char *buf, char *end, const char *s, - struct printf_spec spec) +void string(struct printbuf *out, + const char *s, + struct printf_spec spec) { - if (check_pointer(&buf, end, s, spec)) - return buf; + if (check_pointer(out, s, spec)) + return; =20 - return string_nocheck(buf, end, s, spec); + string_nocheck(out, s, spec); } =20 -static char *pointer_string(char *buf, char *end, - const void *ptr, - struct printf_spec spec) +static void pointer_string(struct printbuf *out, + const void *ptr, + struct printf_spec spec) { spec.base =3D 16; spec.flags |=3D SMALL; @@ -731,7 +698,7 @@ static char *pointer_string(char *buf, char *end, spec.flags |=3D ZEROPAD; } =20 - return number(buf, end, (unsigned long int)ptr, spec); + number(out, (unsigned long int)ptr, spec); } =20 /* Make pointers available for printing early in the boot sequence. */ @@ -818,8 +785,9 @@ int ptr_to_hashval(const void *ptr, unsigned long *hash= val_out) return __ptr_to_hashval(ptr, hashval_out); } =20 -static char *ptr_to_id(char *buf, char *end, const void *ptr, - struct printf_spec spec) +static void ptr_to_id(struct printbuf *out, + const void *ptr, + struct printf_spec spec) { const char *str =3D sizeof(ptr) =3D=3D 8 ? "(____ptrval____)" : "(ptrval)= "; unsigned long hashval; @@ -830,34 +798,35 @@ static char *ptr_to_id(char *buf, char *end, const vo= id *ptr, * as they are not actual addresses. */ if (IS_ERR_OR_NULL(ptr)) - return pointer_string(buf, end, ptr, spec); + return pointer_string(out, ptr, spec); =20 /* When debugging early boot use non-cryptographically secure hash. */ if (unlikely(debug_boot_weak_hash)) { hashval =3D hash_long((unsigned long)ptr, 32); - return pointer_string(buf, end, (const void *)hashval, spec); + return pointer_string(out, (const void *)hashval, spec); } =20 ret =3D __ptr_to_hashval(ptr, &hashval); if (ret) { spec.field_width =3D 2 * sizeof(ptr); /* string length must be less than default_width */ - return error_string(buf, end, str, spec); + return error_string(out, str, spec); } =20 - return pointer_string(buf, end, (const void *)hashval, spec); + pointer_string(out, (const void *)hashval, spec); } =20 int kptr_restrict __read_mostly; =20 static noinline_for_stack -char *restricted_pointer(char *buf, char *end, const void *ptr, - struct printf_spec spec) +void restricted_pointer(struct printbuf *out, + const void *ptr, + struct printf_spec spec) { switch (kptr_restrict) { case 0: /* Handle as %p, hash and do _not_ leak addresses. */ - return ptr_to_id(buf, end, ptr, spec); + return ptr_to_id(out, ptr, spec); case 1: { const struct cred *cred; =20 @@ -868,7 +837,7 @@ char *restricted_pointer(char *buf, char *end, const vo= id *ptr, if (in_irq() || in_serving_softirq() || in_nmi()) { if (spec.field_width =3D=3D -1) spec.field_width =3D 2 * sizeof(ptr); - return error_string(buf, end, "pK-error", spec); + return error_string(out, "pK-error", spec); } =20 /* @@ -894,12 +863,13 @@ char *restricted_pointer(char *buf, char *end, const = void *ptr, break; } =20 - return pointer_string(buf, end, ptr, spec); + return pointer_string(out, ptr, spec); } =20 static noinline_for_stack -char *dentry_name(char *buf, char *end, const struct dentry *d, struct pri= ntf_spec spec, - const char *fmt) +void dentry_name(struct printbuf *out, + const struct dentry *d, struct printf_spec spec, + const char *fmt) { const char *array[4], *s; const struct dentry *p; @@ -916,9 +886,9 @@ char *dentry_name(char *buf, char *end, const struct de= ntry *d, struct printf_sp =20 rcu_read_lock(); for (i =3D 0; i < depth; i++, d =3D p) { - if (check_pointer(&buf, end, d, spec)) { + if (check_pointer(out, d, spec)) { rcu_read_unlock(); - return buf; + return; } =20 p =3D READ_ONCE(d->d_parent); @@ -931,7 +901,7 @@ char *dentry_name(char *buf, char *end, const struct de= ntry *d, struct printf_sp } } s =3D array[--i]; - for (n =3D 0; n !=3D spec.precision; n++, buf++) { + for (n =3D 0; n !=3D spec.precision; n++) { char c =3D *s++; if (!c) { if (!i) @@ -939,49 +909,46 @@ char *dentry_name(char *buf, char *end, const struct = dentry *d, struct printf_sp c =3D '/'; s =3D array[--i]; } - if (buf < end) - *buf =3D c; + __pr_char(out, c); } rcu_read_unlock(); - return widen_string(buf, n, end, spec); + widen_string(out, n, spec); } =20 static noinline_for_stack -char *file_dentry_name(char *buf, char *end, const struct file *f, - struct printf_spec spec, const char *fmt) +void file_dentry_name(struct printbuf *out, + const struct file *f, + struct printf_spec spec, const char *fmt) { - if (check_pointer(&buf, end, f, spec)) - return buf; + if (check_pointer(out, f, spec)) + return; =20 - return dentry_name(buf, end, f->f_path.dentry, spec, fmt); + return dentry_name(out, f->f_path.dentry, spec, fmt); } #ifdef CONFIG_BLOCK static noinline_for_stack -char *bdev_name(char *buf, char *end, struct block_device *bdev, - struct printf_spec spec, const char *fmt) +void bdev_name(struct printbuf *out, + struct block_device *bdev, + struct printf_spec spec, const char *fmt) { struct gendisk *hd; =20 - if (check_pointer(&buf, end, bdev, spec)) - return buf; + if (check_pointer(out, bdev, spec)) + return; =20 hd =3D bdev->bd_disk; - buf =3D string(buf, end, hd->disk_name, spec); + string(out, hd->disk_name, spec); if (bdev->bd_partno) { - if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) { - if (buf < end) - *buf =3D 'p'; - buf++; - } - buf =3D number(buf, end, bdev->bd_partno, spec); + if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) + __pr_char(out, 'p'); + number(out, bdev->bd_partno, spec); } - return buf; } #endif =20 static noinline_for_stack -char *symbol_string(char *buf, char *end, void *ptr, - struct printf_spec spec, const char *fmt) +void symbol_string(struct printbuf *out, void *ptr, + struct printf_spec spec, const char *fmt) { unsigned long value; #ifdef CONFIG_KALLSYMS @@ -1004,9 +971,9 @@ char *symbol_string(char *buf, char *end, void *ptr, else sprint_symbol_no_offset(sym, value); =20 - return string_nocheck(buf, end, sym, spec); + string_nocheck(out, sym, spec); #else - return special_hex_number(buf, end, value, sizeof(void *)); + special_hex_number(out, value, sizeof(void *)); #endif } =20 @@ -1041,8 +1008,8 @@ static const struct printf_spec default_dec04_spec = =3D { }; =20 static noinline_for_stack -char *resource_string(char *buf, char *end, struct resource *res, - struct printf_spec spec, const char *fmt) +void resource_string(struct printbuf *out, struct resource *res, + struct printf_spec spec, const char *fmt) { #ifndef IO_RSRC_PRINTK_SIZE #define IO_RSRC_PRINTK_SIZE 6 @@ -1081,69 +1048,68 @@ char *resource_string(char *buf, char *end, struct = resource *res, #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") - char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, + char sym_buf[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; - - char *p =3D sym, *pend =3D sym + sizeof(sym); + struct printbuf sym =3D PRINTBUF_EXTERN(sym_buf, sizeof(sym_buf)); int decode =3D (fmt[0] =3D=3D 'R') ? 1 : 0; const struct printf_spec *specp; =20 - if (check_pointer(&buf, end, res, spec)) - return buf; + if (check_pointer(out, res, spec)) + return; =20 - *p++ =3D '['; + __pr_char(&sym, '['); if (res->flags & IORESOURCE_IO) { - p =3D string_nocheck(p, pend, "io ", str_spec); + string_nocheck(&sym, "io ", str_spec); specp =3D &io_spec; } else if (res->flags & IORESOURCE_MEM) { - p =3D string_nocheck(p, pend, "mem ", str_spec); + string_nocheck(&sym, "mem ", str_spec); specp =3D &mem_spec; } else if (res->flags & IORESOURCE_IRQ) { - p =3D string_nocheck(p, pend, "irq ", str_spec); + string_nocheck(&sym, "irq ", str_spec); specp =3D &default_dec_spec; } else if (res->flags & IORESOURCE_DMA) { - p =3D string_nocheck(p, pend, "dma ", str_spec); + string_nocheck(&sym, "dma ", str_spec); specp =3D &default_dec_spec; } else if (res->flags & IORESOURCE_BUS) { - p =3D string_nocheck(p, pend, "bus ", str_spec); + string_nocheck(&sym, "bus ", str_spec); specp =3D &bus_spec; } else { - p =3D string_nocheck(p, pend, "??? ", str_spec); + string_nocheck(&sym, "??? ", str_spec); specp =3D &mem_spec; decode =3D 0; } if (decode && res->flags & IORESOURCE_UNSET) { - p =3D string_nocheck(p, pend, "size ", str_spec); - p =3D number(p, pend, resource_size(res), *specp); + string_nocheck(&sym, "size ", str_spec); + number(&sym, resource_size(res), *specp); } else { - p =3D number(p, pend, res->start, *specp); + number(&sym, res->start, *specp); if (res->start !=3D res->end) { - *p++ =3D '-'; - p =3D number(p, pend, res->end, *specp); + __pr_char(&sym, '-'); + number(&sym, res->end, *specp); } } if (decode) { if (res->flags & IORESOURCE_MEM_64) - p =3D string_nocheck(p, pend, " 64bit", str_spec); + string_nocheck(&sym, " 64bit", str_spec); if (res->flags & IORESOURCE_PREFETCH) - p =3D string_nocheck(p, pend, " pref", str_spec); + string_nocheck(&sym, " pref", str_spec); if (res->flags & IORESOURCE_WINDOW) - p =3D string_nocheck(p, pend, " window", str_spec); + string_nocheck(&sym, " window", str_spec); if (res->flags & IORESOURCE_DISABLED) - p =3D string_nocheck(p, pend, " disabled", str_spec); + string_nocheck(&sym, " disabled", str_spec); } else { - p =3D string_nocheck(p, pend, " flags ", str_spec); - p =3D number(p, pend, res->flags, default_flag_spec); + string_nocheck(&sym, " flags ", str_spec); + number(&sym, res->flags, default_flag_spec); } - *p++ =3D ']'; - *p =3D '\0'; + __pr_char(&sym, ']'); + printbuf_nul_terminate(&sym); =20 - return string_nocheck(buf, end, sym, spec); + string_nocheck(out, sym_buf, spec); } =20 static noinline_for_stack -char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, - const char *fmt) +void hex_string(struct printbuf *out, u8 *addr, + struct printf_spec spec, const char *fmt) { int i, len =3D 1; /* if we pass '%ph[CDN]', field width remains negative value, fallback to the default */ @@ -1151,10 +1117,10 @@ char *hex_string(char *buf, char *end, u8 *addr, st= ruct printf_spec spec, =20 if (spec.field_width =3D=3D 0) /* nothing to print */ - return buf; + return; =20 - if (check_pointer(&buf, end, addr, spec)) - return buf; + if (check_pointer(out, addr, spec)) + return; =20 switch (fmt[1]) { case 'C': @@ -1175,34 +1141,25 @@ char *hex_string(char *buf, char *end, u8 *addr, st= ruct printf_spec spec, len =3D min_t(int, spec.field_width, 64); =20 for (i =3D 0; i < len; ++i) { - if (buf < end) - *buf =3D hex_asc_hi(addr[i]); - ++buf; - if (buf < end) - *buf =3D hex_asc_lo(addr[i]); - ++buf; - - if (separator && i !=3D len - 1) { - if (buf < end) - *buf =3D separator; - ++buf; - } - } + __pr_char(out, hex_asc_hi(addr[i])); + __pr_char(out, hex_asc_lo(addr[i])); =20 - return buf; + if (separator && i !=3D len - 1) + __pr_char(out, separator); + } } =20 static noinline_for_stack -char *bitmap_string(char *buf, char *end, unsigned long *bitmap, - struct printf_spec spec, const char *fmt) +void bitmap_string(struct printbuf *out, unsigned long *bitmap, + struct printf_spec spec, const char *fmt) { const int CHUNKSZ =3D 32; int nr_bits =3D max_t(int, spec.field_width, 0); int i, chunksz; bool first =3D true; =20 - if (check_pointer(&buf, end, bitmap, spec)) - return buf; + if (check_pointer(out, bitmap, spec)) + return; =20 /* reused to print numbers */ spec =3D (struct printf_spec){ .flags =3D SMALL | ZEROPAD, .base =3D 16 }; @@ -1221,54 +1178,45 @@ char *bitmap_string(char *buf, char *end, unsigned = long *bitmap, bit =3D i % BITS_PER_LONG; val =3D (bitmap[word] >> bit) & chunkmask; =20 - if (!first) { - if (buf < end) - *buf =3D ','; - buf++; - } + if (!first) + __pr_char(out, ','); first =3D false; =20 spec.field_width =3D DIV_ROUND_UP(chunksz, 4); - buf =3D number(buf, end, val, spec); + number(out, val, spec); =20 chunksz =3D CHUNKSZ; } - return buf; } =20 static noinline_for_stack -char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, - struct printf_spec spec, const char *fmt) +void bitmap_list_string(struct printbuf *out, unsigned long *bitmap, + struct printf_spec spec, const char *fmt) { int nr_bits =3D max_t(int, spec.field_width, 0); bool first =3D true; int rbot, rtop; =20 - if (check_pointer(&buf, end, bitmap, spec)) - return buf; + if (check_pointer(out, bitmap, spec)) + return ; =20 for_each_set_bitrange(rbot, rtop, bitmap, nr_bits) { - if (!first) { - if (buf < end) - *buf =3D ','; - buf++; - } + if (!first) + __pr_char(out, ','); first =3D false; =20 - buf =3D number(buf, end, rbot, default_dec_spec); + number(out, rbot, default_dec_spec); if (rtop =3D=3D rbot + 1) continue; =20 - if (buf < end) - *buf =3D '-'; - buf =3D number(++buf, end, rtop - 1, default_dec_spec); + __pr_char(out, '-'); + number(out, rtop - 1, default_dec_spec); } - return buf; } =20 static noinline_for_stack -char *mac_address_string(char *buf, char *end, u8 *addr, - struct printf_spec spec, const char *fmt) +void mac_address_string(struct printbuf *out, u8 *addr, + struct printf_spec spec, const char *fmt) { char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; char *p =3D mac_addr; @@ -1276,8 +1224,8 @@ char *mac_address_string(char *buf, char *end, u8 *ad= dr, char separator; bool reversed =3D false; =20 - if (check_pointer(&buf, end, addr, spec)) - return buf; + if (check_pointer(out, addr, spec)) + return; =20 switch (fmt[1]) { case 'F': @@ -1304,11 +1252,12 @@ char *mac_address_string(char *buf, char *end, u8 *= addr, } *p =3D '\0'; =20 - return string_nocheck(buf, end, mac_addr, spec); + string_nocheck(out, mac_addr, spec); } =20 static noinline_for_stack -char *ip4_string(char *p, const u8 *addr, const char *fmt) +void ip4_string(struct printbuf *out, + const u8 *addr, const char *fmt) { int i; bool leading_zeros =3D (fmt[0] =3D=3D 'i'); @@ -1341,24 +1290,23 @@ char *ip4_string(char *p, const u8 *addr, const cha= r *fmt) int digits =3D put_dec_trunc8(temp, addr[index]) - temp; if (leading_zeros) { if (digits < 3) - *p++ =3D '0'; + __pr_char(out, '0'); if (digits < 2) - *p++ =3D '0'; + __pr_char(out, '0'); } /* reverse the digits in the quad */ while (digits--) - *p++ =3D temp[digits]; + __pr_char(out, temp[digits]); if (i < 3) - *p++ =3D '.'; + __pr_char(out, '.'); index +=3D step; } - *p =3D '\0'; =20 - return p; + printbuf_nul_terminate(out); } =20 static noinline_for_stack -char *ip6_compressed_string(char *p, const char *addr) +void ip6_compressed_string(struct printbuf *out, const char *addr) { int i, j, range; unsigned char zerolength[8]; @@ -1402,14 +1350,14 @@ char *ip6_compressed_string(char *p, const char *ad= dr) for (i =3D 0; i < range; i++) { if (i =3D=3D colonpos) { if (needcolon || i =3D=3D 0) - *p++ =3D ':'; - *p++ =3D ':'; + __pr_char(out, ':'); + __pr_char(out, ':'); needcolon =3D false; i +=3D longest - 1; continue; } if (needcolon) { - *p++ =3D ':'; + __pr_char(out, ':'); needcolon =3D false; } /* hex u16 without leading 0s */ @@ -1418,81 +1366,81 @@ char *ip6_compressed_string(char *p, const char *ad= dr) lo =3D word & 0xff; if (hi) { if (hi > 0x0f) - p =3D hex_byte_pack(p, hi); + pr_hex_byte(out, hi); else - *p++ =3D hex_asc_lo(hi); - p =3D hex_byte_pack(p, lo); + __pr_char(out, hex_asc_lo(hi)); + pr_hex_byte(out, lo); } else if (lo > 0x0f) - p =3D hex_byte_pack(p, lo); + pr_hex_byte(out, lo); else - *p++ =3D hex_asc_lo(lo); + __pr_char(out, hex_asc_lo(lo)); needcolon =3D true; } =20 if (useIPv4) { if (needcolon) - *p++ =3D ':'; - p =3D ip4_string(p, &in6.s6_addr[12], "I4"); + __pr_char(out, ':'); + ip4_string(out, &in6.s6_addr[12], "I4"); } - *p =3D '\0'; =20 - return p; + printbuf_nul_terminate(out); } =20 static noinline_for_stack -char *ip6_string(char *p, const char *addr, const char *fmt) +void ip6_string(struct printbuf *out, const char *addr, const char *fmt) { int i; =20 for (i =3D 0; i < 8; i++) { - p =3D hex_byte_pack(p, *addr++); - p =3D hex_byte_pack(p, *addr++); + pr_hex_byte(out, *addr++); + pr_hex_byte(out, *addr++); if (fmt[0] =3D=3D 'I' && i !=3D 7) - *p++ =3D ':'; + __pr_char(out, ':'); } - *p =3D '\0'; =20 - return p; + printbuf_nul_terminate(out); } =20 static noinline_for_stack -char *ip6_addr_string(char *buf, char *end, const u8 *addr, - struct printf_spec spec, const char *fmt) +void ip6_addr_string(struct printbuf *out, const u8 *addr, + struct printf_spec spec, const char *fmt) { - char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; + char ip6_addr_buf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")= ]; + struct printbuf ip6_addr =3D PRINTBUF_EXTERN(ip6_addr_buf, sizeof(ip6_add= r_buf)); =20 if (fmt[0] =3D=3D 'I' && fmt[2] =3D=3D 'c') - ip6_compressed_string(ip6_addr, addr); + ip6_compressed_string(&ip6_addr, addr); else - ip6_string(ip6_addr, addr, fmt); + ip6_string(&ip6_addr, addr, fmt); =20 - return string_nocheck(buf, end, ip6_addr, spec); + string_nocheck(out, ip6_addr_buf, spec); } =20 static noinline_for_stack -char *ip4_addr_string(char *buf, char *end, const u8 *addr, - struct printf_spec spec, const char *fmt) +void ip4_addr_string(struct printbuf *out, const u8 *addr, + struct printf_spec spec, const char *fmt) { - char ip4_addr[sizeof("255.255.255.255")]; + char ip4_addr_buf[sizeof("255.255.255.255")]; + struct printbuf ip4_addr =3D PRINTBUF_EXTERN(ip4_addr_buf, sizeof(ip4_add= r_buf)); =20 - ip4_string(ip4_addr, addr, fmt); + ip4_string(&ip4_addr, addr, fmt); =20 - return string_nocheck(buf, end, ip4_addr, spec); + string_nocheck(out, ip4_addr_buf, spec); } =20 static noinline_for_stack -char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *= sa, - struct printf_spec spec, const char *fmt) +void ip6_addr_string_sa(struct printbuf *out, + const struct sockaddr_in6 *sa, + struct printf_spec spec, const char *fmt) { bool have_p =3D false, have_s =3D false, have_f =3D false, have_c =3D fal= se; - char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + - sizeof(":12345") + sizeof("/123456789") + - sizeof("%1234567890")]; - char *p =3D ip6_addr, *pend =3D ip6_addr + sizeof(ip6_addr); + char ip6_addr_buf[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]= ") + + sizeof(":12345") + sizeof("/123456789") + + sizeof("%1234567890")]; + struct printbuf ip6_addr =3D PRINTBUF_EXTERN(ip6_addr_buf, sizeof(ip6_add= r_buf)); const u8 *addr =3D (const u8 *) &sa->sin6_addr; char fmt6[2] =3D { fmt[0], '6' }; - u8 off =3D 0; =20 fmt++; while (isalpha(*++fmt)) { @@ -1512,44 +1460,43 @@ char *ip6_addr_string_sa(char *buf, char *end, cons= t struct sockaddr_in6 *sa, } } =20 - if (have_p || have_s || have_f) { - *p =3D '['; - off =3D 1; - } + if (have_p || have_s || have_f) + __pr_char(&ip6_addr, '['); =20 if (fmt6[0] =3D=3D 'I' && have_c) - p =3D ip6_compressed_string(ip6_addr + off, addr); + ip6_compressed_string(&ip6_addr, addr); else - p =3D ip6_string(ip6_addr + off, addr, fmt6); + ip6_string(&ip6_addr, addr, fmt6); =20 if (have_p || have_s || have_f) - *p++ =3D ']'; + __pr_char(&ip6_addr, ']'); =20 if (have_p) { - *p++ =3D ':'; - p =3D number(p, pend, ntohs(sa->sin6_port), spec); + __pr_char(&ip6_addr, ':'); + number(&ip6_addr, ntohs(sa->sin6_port), spec); } if (have_f) { - *p++ =3D '/'; - p =3D number(p, pend, ntohl(sa->sin6_flowinfo & - IPV6_FLOWINFO_MASK), spec); + __pr_char(&ip6_addr, '/'); + number(&ip6_addr, ntohl(sa->sin6_flowinfo & + IPV6_FLOWINFO_MASK), spec); } if (have_s) { - *p++ =3D '%'; - p =3D number(p, pend, sa->sin6_scope_id, spec); + __pr_char(&ip6_addr, '%'); + number(&ip6_addr, sa->sin6_scope_id, spec); } - *p =3D '\0'; + printbuf_nul_terminate(&ip6_addr); =20 - return string_nocheck(buf, end, ip6_addr, spec); + string_nocheck(out, ip6_addr_buf, spec); } =20 static noinline_for_stack -char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *s= a, - struct printf_spec spec, const char *fmt) +void ip4_addr_string_sa(struct printbuf *out, + const struct sockaddr_in *sa, + struct printf_spec spec, const char *fmt) { bool have_p =3D false; - char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; - char *pend =3D ip4_addr + sizeof(ip4_addr); + char ip4_addr_buf[sizeof("255.255.255.255") + sizeof(":12345")]; + struct printbuf ip4_addr =3D PRINTBUF_EXTERN(ip4_addr_buf, sizeof(ip4_add= r_buf)); const u8 *addr =3D (const u8 *) &sa->sin_addr.s_addr; char fmt4[3] =3D { fmt[0], '4', 0 }; =20 @@ -1568,30 +1515,30 @@ char *ip4_addr_string_sa(char *buf, char *end, cons= t struct sockaddr_in *sa, } } =20 - p =3D ip4_string(ip4_addr, addr, fmt4); + ip4_string(&ip4_addr, addr, fmt4); if (have_p) { - *p++ =3D ':'; - p =3D number(p, pend, ntohs(sa->sin_port), spec); + __pr_char(&ip4_addr, ':'); + number(&ip4_addr, ntohs(sa->sin_port), spec); } - *p =3D '\0'; + printbuf_nul_terminate(&ip4_addr); =20 - return string_nocheck(buf, end, ip4_addr, spec); + string_nocheck(out, ip4_addr_buf, spec); } =20 static noinline_for_stack -char *ip_addr_string(char *buf, char *end, const void *ptr, - struct printf_spec spec, const char *fmt) +void ip_addr_string(struct printbuf *out, const void *ptr, + struct printf_spec spec, const char *fmt) { char *err_fmt_msg; =20 - if (check_pointer(&buf, end, ptr, spec)) - return buf; + if (check_pointer(out, ptr, spec)) + return; =20 switch (fmt[1]) { case '6': - return ip6_addr_string(buf, end, ptr, spec, fmt); + return ip6_addr_string(out, ptr, spec, fmt); case '4': - return ip4_addr_string(buf, end, ptr, spec, fmt); + return ip4_addr_string(out, ptr, spec, fmt); case 'S': { const union { struct sockaddr raw; @@ -1601,21 +1548,21 @@ char *ip_addr_string(char *buf, char *end, const vo= id *ptr, =20 switch (sa->raw.sa_family) { case AF_INET: - return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); + return ip4_addr_string_sa(out, &sa->v4, spec, fmt); case AF_INET6: - return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); + return ip6_addr_string_sa(out, &sa->v6, spec, fmt); default: - return error_string(buf, end, "(einval)", spec); + return error_string(out, "(einval)", spec); }} } =20 err_fmt_msg =3D fmt[0] =3D=3D 'i' ? "(%pi?)" : "(%pI?)"; - return error_string(buf, end, err_fmt_msg, spec); + return error_string(out, err_fmt_msg, spec); } =20 static noinline_for_stack -char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec sp= ec, - const char *fmt) +void escaped_string(struct printbuf *out, u8 *addr, + struct printf_spec spec, const char *fmt) { bool found =3D true; int count =3D 1; @@ -1623,10 +1570,10 @@ char *escaped_string(char *buf, char *end, u8 *addr= , struct printf_spec spec, int len; =20 if (spec.field_width =3D=3D 0) - return buf; /* nothing to print */ + return; /* nothing to print */ =20 - if (check_pointer(&buf, end, addr, spec)) - return buf; + if (check_pointer(out, addr, spec)) + return; =20 do { switch (fmt[count++]) { @@ -1661,44 +1608,35 @@ char *escaped_string(char *buf, char *end, u8 *addr= , struct printf_spec spec, flags =3D ESCAPE_ANY_NP; =20 len =3D spec.field_width < 0 ? 1 : spec.field_width; - - /* - * string_escape_mem() writes as many characters as it can to - * the given buffer, and returns the total size of the output - * had the buffer been big enough. - */ - buf +=3D string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, fla= gs, NULL); - - return buf; + pr_escaped_string(out, addr, len, flags, NULL); } =20 -static char *va_format(char *buf, char *end, struct va_format *va_fmt, - struct printf_spec spec, const char *fmt) +static void va_format(struct printbuf *out, + struct va_format *va_fmt, + struct printf_spec spec, const char *fmt) { va_list va; =20 - if (check_pointer(&buf, end, va_fmt, spec)) - return buf; + if (check_pointer(out, va_fmt, spec)) + return; =20 va_copy(va, *va_fmt->va); - buf +=3D vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va); + vpr_buf(out, va_fmt->fmt, va); va_end(va); - - return buf; } =20 static noinline_for_stack -char *uuid_string(char *buf, char *end, const u8 *addr, - struct printf_spec spec, const char *fmt) +void uuid_string(struct printbuf *out, const u8 *addr, + struct printf_spec spec, const char *fmt) { - char uuid[UUID_STRING_LEN + 1]; - char *p =3D uuid; + char uuid_buf[UUID_STRING_LEN + 1]; + struct printbuf uuid =3D PRINTBUF_EXTERN(uuid_buf, sizeof(uuid_buf)); int i; const u8 *index =3D uuid_index; bool uc =3D false; =20 - if (check_pointer(&buf, end, addr, spec)) - return buf; + if (check_pointer(out, addr, spec)) + return; =20 switch (*(++fmt)) { case 'L': @@ -1714,60 +1652,60 @@ char *uuid_string(char *buf, char *end, const u8 *a= ddr, =20 for (i =3D 0; i < 16; i++) { if (uc) - p =3D hex_byte_pack_upper(p, addr[index[i]]); + pr_hex_byte_upper(&uuid, addr[index[i]]); else - p =3D hex_byte_pack(p, addr[index[i]]); + pr_hex_byte(&uuid, addr[index[i]]); switch (i) { case 3: case 5: case 7: case 9: - *p++ =3D '-'; + __pr_char(&uuid, '-'); break; } } =20 - *p =3D 0; + printbuf_nul_terminate(&uuid); =20 - return string_nocheck(buf, end, uuid, spec); + string_nocheck(out, uuid_buf, spec); } =20 static noinline_for_stack -char *netdev_bits(char *buf, char *end, const void *addr, - struct printf_spec spec, const char *fmt) +void netdev_bits(struct printbuf *out, const void *addr, + struct printf_spec spec, const char *fmt) { unsigned long long num; int size; =20 - if (check_pointer(&buf, end, addr, spec)) - return buf; + if (check_pointer(out, addr, spec)) + return; =20 switch (fmt[1]) { case 'F': num =3D *(const netdev_features_t *)addr; size =3D sizeof(netdev_features_t); + special_hex_number(out, num, size); break; default: - return error_string(buf, end, "(%pN?)", spec); + error_string(out, "(%pN?)", spec); + break; } - - return special_hex_number(buf, end, num, size); } =20 static noinline_for_stack -char *fourcc_string(char *buf, char *end, const u32 *fourcc, - struct printf_spec spec, const char *fmt) +void fourcc_string(struct printbuf *out, const u32 *fourcc, + struct printf_spec spec, const char *fmt) { - char output[sizeof("0123 little-endian (0x01234567)")]; - char *p =3D output; + char output_buf[sizeof("0123 little-endian (0x01234567)")]; + struct printbuf output =3D PRINTBUF_EXTERN(output_buf, sizeof(output_buf)= ); unsigned int i; u32 val; =20 if (fmt[1] !=3D 'c' || fmt[2] !=3D 'c') - return error_string(buf, end, "(%p4?)", spec); + return error_string(out, "(%p4?)", spec); =20 - if (check_pointer(&buf, end, fourcc, spec)) - return buf; + if (check_pointer(out, fourcc, spec)) + return; =20 val =3D *fourcc & ~BIT(31); =20 @@ -1775,30 +1713,29 @@ char *fourcc_string(char *buf, char *end, const u32= *fourcc, unsigned char c =3D val >> (i * 8); =20 /* Print non-control ASCII characters as-is, dot otherwise */ - *p++ =3D isascii(c) && isprint(c) ? c : '.'; + __pr_char(&output, isascii(c) && isprint(c) ? c : '.'); } =20 - strcpy(p, *fourcc & BIT(31) ? " big-endian" : " little-endian"); - p +=3D strlen(p); + pr_str(&output, *fourcc & BIT(31) ? " big-endian" : " little-endian"); =20 - *p++ =3D ' '; - *p++ =3D '('; - p =3D special_hex_number(p, output + sizeof(output) - 2, *fourcc, sizeof(= u32)); - *p++ =3D ')'; - *p =3D '\0'; + __pr_char(&output, ' '); + __pr_char(&output, '('); + special_hex_number(&output, *fourcc, sizeof(u32)); + __pr_char(&output, ')'); + printbuf_nul_terminate(&output); =20 - return string(buf, end, output, spec); + string(out, output_buf, spec); } =20 static noinline_for_stack -char *address_val(char *buf, char *end, const void *addr, - struct printf_spec spec, const char *fmt) +void address_val(struct printbuf *out, const void *addr, + struct printf_spec spec, const char *fmt) { unsigned long long num; int size; =20 - if (check_pointer(&buf, end, addr, spec)) - return buf; + if (check_pointer(out, addr, spec)) + return; =20 switch (fmt[1]) { case 'd': @@ -1812,55 +1749,44 @@ char *address_val(char *buf, char *end, const void = *addr, break; } =20 - return special_hex_number(buf, end, num, size); + special_hex_number(out, num, size); } =20 static noinline_for_stack -char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r) +void date_str(struct printbuf *out, + const struct rtc_time *tm, bool r) { int year =3D tm->tm_year + (r ? 0 : 1900); int mon =3D tm->tm_mon + (r ? 0 : 1); =20 - buf =3D number(buf, end, year, default_dec04_spec); - if (buf < end) - *buf =3D '-'; - buf++; - - buf =3D number(buf, end, mon, default_dec02_spec); - if (buf < end) - *buf =3D '-'; - buf++; - - return number(buf, end, tm->tm_mday, default_dec02_spec); + number(out, year, default_dec04_spec); + __pr_char(out, '-'); + number(out, mon, default_dec02_spec); + __pr_char(out, '-'); + number(out, tm->tm_mday, default_dec02_spec); } =20 static noinline_for_stack -char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r) +void time_str(struct printbuf *out, const struct rtc_time *tm, bool r) { - buf =3D number(buf, end, tm->tm_hour, default_dec02_spec); - if (buf < end) - *buf =3D ':'; - buf++; - - buf =3D number(buf, end, tm->tm_min, default_dec02_spec); - if (buf < end) - *buf =3D ':'; - buf++; - - return number(buf, end, tm->tm_sec, default_dec02_spec); + number(out, tm->tm_hour, default_dec02_spec); + __pr_char(out, ':'); + number(out, tm->tm_min, default_dec02_spec); + __pr_char(out, ':'); + number(out, tm->tm_sec, default_dec02_spec); } =20 static noinline_for_stack -char *rtc_str(char *buf, char *end, const struct rtc_time *tm, - struct printf_spec spec, const char *fmt) +void rtc_str(struct printbuf *out, const struct rtc_time *tm, + struct printf_spec spec, const char *fmt) { bool have_t =3D true, have_d =3D true; bool raw =3D false, iso8601_separator =3D true; bool found =3D true; int count =3D 2; =20 - if (check_pointer(&buf, end, tm, spec)) - return buf; + if (check_pointer(out, tm, spec)) + return; =20 switch (fmt[count]) { case 'd': @@ -1888,21 +1814,16 @@ char *rtc_str(char *buf, char *end, const struct rt= c_time *tm, } while (found); =20 if (have_d) - buf =3D date_str(buf, end, tm, raw); - if (have_d && have_t) { - if (buf < end) - *buf =3D iso8601_separator ? 'T' : ' '; - buf++; - } + date_str(out, tm, raw); + if (have_d && have_t) + __pr_char(out, iso8601_separator ? 'T' : ' '); if (have_t) - buf =3D time_str(buf, end, tm, raw); - - return buf; + time_str(out, tm, raw); } =20 static noinline_for_stack -char *time64_str(char *buf, char *end, const time64_t time, - struct printf_spec spec, const char *fmt) +void time64_str(struct printbuf *out, const time64_t time, + struct printf_spec spec, const char *fmt) { struct rtc_time rtc_time; struct tm tm; @@ -1920,47 +1841,48 @@ char *time64_str(char *buf, char *end, const time64= _t time, =20 rtc_time.tm_isdst =3D 0; =20 - return rtc_str(buf, end, &rtc_time, spec, fmt); + rtc_str(out, &rtc_time, spec, fmt); } =20 static noinline_for_stack -char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec sp= ec, - const char *fmt) +void time_and_date(struct printbuf *out, + void *ptr, struct printf_spec spec, + const char *fmt) { switch (fmt[1]) { case 'R': - return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt); + return rtc_str(out, (const struct rtc_time *)ptr, spec, fmt); case 'T': - return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt); + return time64_str(out, *(const time64_t *)ptr, spec, fmt); default: - return error_string(buf, end, "(%pt?)", spec); + return error_string(out, "(%pt?)", spec); } } =20 static noinline_for_stack -char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, - const char *fmt) +void clock(struct printbuf *out, struct clk *clk, + struct printf_spec spec, const char *fmt) { if (!IS_ENABLED(CONFIG_HAVE_CLK)) - return error_string(buf, end, "(%pC?)", spec); + return error_string(out, "(%pC?)", spec); =20 - if (check_pointer(&buf, end, clk, spec)) - return buf; + if (check_pointer(out, clk, spec)) + return; =20 switch (fmt[1]) { case 'n': default: #ifdef CONFIG_COMMON_CLK - return string(buf, end, __clk_get_name(clk), spec); + return string(out, __clk_get_name(clk), spec); #else - return ptr_to_id(buf, end, clk, spec); + return ptr_to_id(out, clk, spec); #endif } } =20 static -char *format_flags(char *buf, char *end, unsigned long flags, - const struct trace_print_flags *names) +void format_flags(struct printbuf *out, unsigned long flags, + const struct trace_print_flags *names) { unsigned long mask; =20 @@ -1969,20 +1891,15 @@ char *format_flags(char *buf, char *end, unsigned l= ong flags, if ((flags & mask) !=3D mask) continue; =20 - buf =3D string(buf, end, names->name, default_str_spec); + string(out, names->name, default_str_spec); =20 flags &=3D ~mask; - if (flags) { - if (buf < end) - *buf =3D '|'; - buf++; - } + if (flags) + __pr_char(out, '|'); } =20 if (flags) - buf =3D number(buf, end, flags, default_flag_spec); - - return buf; + number(out, flags, default_flag_spec); } =20 struct page_flags_fields { @@ -2007,20 +1924,18 @@ static const struct page_flags_fields pff[] =3D { }; =20 static -char *format_page_flags(char *buf, char *end, unsigned long flags) +void format_page_flags(struct printbuf *out, unsigned long flags) { unsigned long main_flags =3D flags & PAGEFLAGS_MASK; bool append =3D false; int i; =20 - buf =3D number(buf, end, flags, default_flag_spec); - if (buf < end) - *buf =3D '('; - buf++; + number(out, flags, default_flag_spec); + __pr_char(out, '('); =20 /* Page flags from the main area. */ if (main_flags) { - buf =3D format_flags(buf, end, main_flags, pageflag_names); + format_flags(out, main_flags, pageflag_names); append =3D true; } =20 @@ -2031,41 +1946,31 @@ char *format_page_flags(char *buf, char *end, unsig= ned long flags) continue; =20 /* Format: Flag Name + '=3D' (equals sign) + Number + '|' (separator) */ - if (append) { - if (buf < end) - *buf =3D '|'; - buf++; - } + if (append) + __pr_char(out, '|'); =20 - buf =3D string(buf, end, pff[i].name, default_str_spec); - if (buf < end) - *buf =3D '=3D'; - buf++; - buf =3D number(buf, end, (flags >> pff[i].shift) & pff[i].mask, - *pff[i].spec); + string(out, pff[i].name, default_str_spec); + __pr_char(out, '=3D'); + number(out, (flags >> pff[i].shift) & pff[i].mask, *pff[i].spec); =20 append =3D true; } - if (buf < end) - *buf =3D ')'; - buf++; - - return buf; + __pr_char(out, ')'); } =20 static noinline_for_stack -char *flags_string(char *buf, char *end, void *flags_ptr, - struct printf_spec spec, const char *fmt) +void flags_string(struct printbuf *out, void *flags_ptr, + struct printf_spec spec, const char *fmt) { unsigned long flags; const struct trace_print_flags *names; =20 - if (check_pointer(&buf, end, flags_ptr, spec)) - return buf; + if (check_pointer(out, flags_ptr, spec)) + return; =20 switch (fmt[1]) { case 'p': - return format_page_flags(buf, end, *(unsigned long *)flags_ptr); + return format_page_flags(out, *(unsigned long *)flags_ptr); case 'v': flags =3D *(unsigned long *)flags_ptr; names =3D vmaflag_names; @@ -2075,15 +1980,15 @@ char *flags_string(char *buf, char *end, void *flag= s_ptr, names =3D gfpflag_names; break; default: - return error_string(buf, end, "(%pG?)", spec); + return error_string(out, "(%pG?)", spec); } =20 - return format_flags(buf, end, flags, names); + return format_flags(out, flags, names); } =20 static noinline_for_stack -char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, - char *end) +void fwnode_full_name_string(struct printbuf *out, + struct fwnode_handle *fwnode) { int depth; =20 @@ -2092,25 +1997,23 @@ char *fwnode_full_name_string(struct fwnode_handle = *fwnode, char *buf, struct fwnode_handle *__fwnode =3D fwnode_get_nth_parent(fwnode, depth); =20 - buf =3D string(buf, end, fwnode_get_name_prefix(__fwnode), - default_str_spec); - buf =3D string(buf, end, fwnode_get_name(__fwnode), - default_str_spec); + string(out, fwnode_get_name_prefix(__fwnode), + default_str_spec); + string(out, fwnode_get_name(__fwnode), + default_str_spec); =20 fwnode_handle_put(__fwnode); } - - return buf; } =20 static noinline_for_stack -char *device_node_string(char *buf, char *end, struct device_node *dn, - struct printf_spec spec, const char *fmt) +void device_node_string(struct printbuf *out, struct device_node *dn, + struct printf_spec spec, const char *fmt) { char tbuf[sizeof("xxxx") + 1]; const char *p; int ret; - char *buf_start =3D buf; + unsigned start =3D out->pos; struct property *prop; bool has_mult, pass; =20 @@ -2118,13 +2021,13 @@ char *device_node_string(char *buf, char *end, stru= ct device_node *dn, str_spec.field_width =3D -1; =20 if (fmt[0] !=3D 'F') - return error_string(buf, end, "(%pO?)", spec); + return error_string(out, "(%pO?)", spec); =20 if (!IS_ENABLED(CONFIG_OF)) - return error_string(buf, end, "(%pOF?)", spec); + return error_string(out, "(%pOF?)", spec); =20 - if (check_pointer(&buf, end, dn, spec)) - return buf; + if (check_pointer(out, dn, spec)) + return; =20 /* simple case without anything any more format specifiers */ fmt++; @@ -2133,32 +2036,28 @@ char *device_node_string(char *buf, char *end, stru= ct device_node *dn, =20 for (pass =3D false; strspn(fmt,"fnpPFcC"); fmt++, pass =3D true) { int precision; - if (pass) { - if (buf < end) - *buf =3D ':'; - buf++; - } + if (pass) + __pr_char(out, ':'); =20 switch (*fmt) { case 'f': /* full_name */ - buf =3D fwnode_full_name_string(of_fwnode_handle(dn), buf, - end); + fwnode_full_name_string(out, of_fwnode_handle(dn)); break; case 'n': /* name */ p =3D fwnode_get_name(of_fwnode_handle(dn)); precision =3D str_spec.precision; str_spec.precision =3D strchrnul(p, '@') - p; - buf =3D string(buf, end, p, str_spec); + string(out, p, str_spec); str_spec.precision =3D precision; break; case 'p': /* phandle */ - buf =3D number(buf, end, (unsigned int)dn->phandle, default_dec_spec); + number(out, (unsigned int)dn->phandle, default_dec_spec); break; case 'P': /* path-spec */ p =3D fwnode_get_name(of_fwnode_handle(dn)); if (!p[1]) p =3D "/"; - buf =3D string(buf, end, p, str_spec); + string(out, p, str_spec); break; case 'F': /* flags */ tbuf[0] =3D of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-'; @@ -2166,21 +2065,21 @@ char *device_node_string(char *buf, char *end, stru= ct device_node *dn, tbuf[2] =3D of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-'; tbuf[3] =3D of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-'; tbuf[4] =3D 0; - buf =3D string_nocheck(buf, end, tbuf, str_spec); + string_nocheck(out, tbuf, str_spec); break; case 'c': /* major compatible string */ ret =3D of_property_read_string(dn, "compatible", &p); if (!ret) - buf =3D string(buf, end, p, str_spec); + string(out, p, str_spec); break; case 'C': /* full compatible string */ has_mult =3D false; of_property_for_each_string(dn, "compatible", prop, p) { if (has_mult) - buf =3D string_nocheck(buf, end, ",", str_spec); - buf =3D string_nocheck(buf, end, "\"", str_spec); - buf =3D string(buf, end, p, str_spec); - buf =3D string_nocheck(buf, end, "\"", str_spec); + string_nocheck(out, ",", str_spec); + string_nocheck(out, "\"", str_spec); + string(out, p, str_spec); + string_nocheck(out, "\"", str_spec); =20 has_mult =3D true; } @@ -2190,37 +2089,38 @@ char *device_node_string(char *buf, char *end, stru= ct device_node *dn, } } =20 - return widen_string(buf, buf - buf_start, end, spec); + widen_string(out, out->pos - start, spec); } =20 static noinline_for_stack -char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode, - struct printf_spec spec, const char *fmt) +void fwnode_string(struct printbuf *out, + struct fwnode_handle *fwnode, + struct printf_spec spec, const char *fmt) { struct printf_spec str_spec =3D spec; - char *buf_start =3D buf; + unsigned start =3D out->pos; =20 str_spec.field_width =3D -1; =20 if (*fmt !=3D 'w') - return error_string(buf, end, "(%pf?)", spec); + return error_string(out, "(%pf?)", spec); =20 - if (check_pointer(&buf, end, fwnode, spec)) - return buf; + if (check_pointer(out, fwnode, spec)) + return; =20 fmt++; =20 switch (*fmt) { case 'P': /* name */ - buf =3D string(buf, end, fwnode_get_name(fwnode), str_spec); + string(out, fwnode_get_name(fwnode), str_spec); break; case 'f': /* full_name */ default: - buf =3D fwnode_full_name_string(fwnode, buf, end); + fwnode_full_name_string(out, fwnode); break; } =20 - return widen_string(buf, buf - buf_start, end, spec); + widen_string(out, out->pos - start, spec); } =20 /* Disable pointer hashing if requested */ @@ -2380,8 +2280,8 @@ early_param("no_hash_pointers", no_hash_pointers_enab= le); * rendering it useful as a unique identifier. */ static noinline_for_stack -char *pointer(const char *fmt, char *buf, char *end, void *ptr, - struct printf_spec spec) +void pointer(struct printbuf *out, const char *fmt, + void *ptr, struct printf_spec spec) { switch (*fmt) { case 'S': @@ -2389,24 +2289,24 @@ char *pointer(const char *fmt, char *buf, char *end= , void *ptr, ptr =3D dereference_symbol_descriptor(ptr); fallthrough; case 'B': - return symbol_string(buf, end, ptr, spec, fmt); + return symbol_string(out, ptr, spec, fmt); case 'R': case 'r': - return resource_string(buf, end, ptr, spec, fmt); + return resource_string(out, ptr, spec, fmt); case 'h': - return hex_string(buf, end, ptr, spec, fmt); + return hex_string(out, ptr, spec, fmt); case 'b': switch (fmt[1]) { case 'l': - return bitmap_list_string(buf, end, ptr, spec, fmt); + return bitmap_list_string(out, ptr, spec, fmt); default: - return bitmap_string(buf, end, ptr, spec, fmt); + return bitmap_string(out, ptr, spec, fmt); } case 'M': /* Colon separated: 00:01:02:03:04:05 */ case 'm': /* Contiguous: 000102030405 */ /* [mM]F (FDDI) */ /* [mM]R (Reverse order; Bluetooth) */ - return mac_address_string(buf, end, ptr, spec, fmt); + return mac_address_string(out, ptr, spec, fmt); case 'I': /* Formatted IP supported * 4: 1.2.3.4 * 6: 0001:0203:...:0708 @@ -2416,54 +2316,54 @@ char *pointer(const char *fmt, char *buf, char *end= , void *ptr, * 4: 001.002.003.004 * 6: 000102...0f */ - return ip_addr_string(buf, end, ptr, spec, fmt); + return ip_addr_string(out, ptr, spec, fmt); case 'E': - return escaped_string(buf, end, ptr, spec, fmt); + return escaped_string(out, ptr, spec, fmt); case 'U': - return uuid_string(buf, end, ptr, spec, fmt); + return uuid_string(out, ptr, spec, fmt); case 'V': - return va_format(buf, end, ptr, spec, fmt); + return va_format(out, ptr, spec, fmt); case 'K': - return restricted_pointer(buf, end, ptr, spec); + return restricted_pointer(out, ptr, spec); case 'N': - return netdev_bits(buf, end, ptr, spec, fmt); + return netdev_bits(out, ptr, spec, fmt); case '4': - return fourcc_string(buf, end, ptr, spec, fmt); + return fourcc_string(out, ptr, spec, fmt); case 'a': - return address_val(buf, end, ptr, spec, fmt); + return address_val(out, ptr, spec, fmt); case 'd': - return dentry_name(buf, end, ptr, spec, fmt); + return dentry_name(out, ptr, spec, fmt); case 't': - return time_and_date(buf, end, ptr, spec, fmt); + return time_and_date(out, ptr, spec, fmt); case 'C': - return clock(buf, end, ptr, spec, fmt); + return clock(out, ptr, spec, fmt); case 'D': - return file_dentry_name(buf, end, ptr, spec, fmt); + return file_dentry_name(out, ptr, spec, fmt); #ifdef CONFIG_BLOCK case 'g': - return bdev_name(buf, end, ptr, spec, fmt); + return bdev_name(out, ptr, spec, fmt); #endif =20 case 'G': - return flags_string(buf, end, ptr, spec, fmt); + return flags_string(out, ptr, spec, fmt); case 'O': - return device_node_string(buf, end, ptr, spec, fmt + 1); + return device_node_string(out, ptr, spec, fmt + 1); case 'f': - return fwnode_string(buf, end, ptr, spec, fmt + 1); + return fwnode_string(out, ptr, spec, fmt + 1); case 'x': - return pointer_string(buf, end, ptr, spec); + return pointer_string(out, ptr, spec); case 'e': /* %pe with a non-ERR_PTR gets treated as plain %p */ if (!IS_ERR(ptr)) break; - return err_ptr(buf, end, ptr, spec); + return err_ptr(out, ptr, spec); case 'u': case 'k': switch (fmt[1]) { case 's': - return string(buf, end, ptr, spec); + return string(out, ptr, spec); default: - return error_string(buf, end, "(einval)", spec); + return error_string(out, "(einval)", spec); } } =20 @@ -2472,9 +2372,9 @@ char *pointer(const char *fmt, char *buf, char *end, = void *ptr, * unless no_hash_pointers is specified on the command line. */ if (unlikely(no_hash_pointers)) - return pointer_string(buf, end, ptr, spec); + return pointer_string(out, ptr, spec); else - return ptr_to_id(buf, end, ptr, spec); + return ptr_to_id(out, ptr, spec); } =20 /* @@ -2695,52 +2595,27 @@ set_precision(struct printf_spec *spec, int prec) } =20 /** - * vsnprintf - Format a string and place it in a buffer - * @buf: The buffer to place the result into - * @size: The size of the buffer, including the trailing null space + * vpr_buf - Format a string, outputting to a printbuf + * @out: The printbuf to output to * @fmt: The format string to use * @args: Arguments for the format string * - * This function generally follows C99 vsnprintf, but has some - * extensions and a few limitations: - * - * - ``%n`` is unsupported - * - ``%p*`` is handled by pointer() - * - * See pointer() or Documentation/core-api/printk-formats.rst for more - * extensive description. + * vpr_buf works much like the traditional vsnprintf(), but outputs to a + * printbuf instead of raw pointer/size. * - * **Please update the documentation in both places when making changes** + * If you're not already dealing with a va_list consider using pr_buf(). * - * The return value is the number of characters which would - * be generated for the given input, excluding the trailing - * '\0', as per ISO C99. If you want to have the exact - * number of characters written into @buf as return value - * (not including the trailing '\0'), use vscnprintf(). If the - * return is greater than or equal to @size, the resulting - * string is truncated. - * - * If you're not already dealing with a va_list consider using snprintf(). + * See the vsnprintf() documentation for format string extensions over C99. */ -int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) +void vpr_buf(struct printbuf *out, const char *fmt, va_list args) { unsigned long long num; - char *str, *end; struct printf_spec spec =3D {0}; =20 /* Reject out-of-range values early. Large positive sizes are used for unknown buffer sizes. */ - if (WARN_ON_ONCE(size > INT_MAX)) - return 0; - - str =3D buf; - end =3D buf + size; - - /* Make sure end is always >=3D buf */ - if (end < buf) { - end =3D ((void *)-1); - size =3D end - buf; - } + if (WARN_ON_ONCE(out->size > INT_MAX)) + return; =20 while (*fmt) { const char *old_fmt =3D fmt; @@ -2749,16 +2624,9 @@ int vsnprintf(char *buf, size_t size, const char *fm= t, va_list args) fmt +=3D read; =20 switch (spec.type) { - case FORMAT_TYPE_NONE: { - int copy =3D read; - if (str < end) { - if (copy > end - str) - copy =3D end - str; - memcpy(str, old_fmt, copy); - } - str +=3D read; + case FORMAT_TYPE_NONE: + pr_bytes(out, old_fmt, read); break; - } =20 case FORMAT_TYPE_WIDTH: set_field_width(&spec, va_arg(args, int)); @@ -2768,44 +2636,29 @@ int vsnprintf(char *buf, size_t size, const char *f= mt, va_list args) set_precision(&spec, va_arg(args, int)); break; =20 - case FORMAT_TYPE_CHAR: { - char c; + case FORMAT_TYPE_CHAR: + if (spec.field_width > 0 && !(spec.flags & LEFT)) + pr_chars(out, spec.field_width, ' '); =20 - if (!(spec.flags & LEFT)) { - while (--spec.field_width > 0) { - if (str < end) - *str =3D ' '; - ++str; + __pr_char(out, (unsigned char) va_arg(args, int)); =20 - } - } - c =3D (unsigned char) va_arg(args, int); - if (str < end) - *str =3D c; - ++str; - while (--spec.field_width > 0) { - if (str < end) - *str =3D ' '; - ++str; - } + if (spec.field_width > 0 && (spec.flags & LEFT)) + pr_chars(out, spec.field_width, ' '); + spec.field_width =3D 0; break; - } =20 case FORMAT_TYPE_STR: - str =3D string(str, end, va_arg(args, char *), spec); + string(out, va_arg(args, char *), spec); break; =20 case FORMAT_TYPE_PTR: - str =3D pointer(fmt, str, end, va_arg(args, void *), - spec); + pointer(out, fmt, va_arg(args, void *), spec); while (isalnum(*fmt)) fmt++; break; =20 case FORMAT_TYPE_PERCENT_CHAR: - if (str < end) - *str =3D '%'; - ++str; + __pr_char(out, '%'); break; =20 case FORMAT_TYPE_INVALID: @@ -2858,21 +2711,71 @@ int vsnprintf(char *buf, size_t size, const char *f= mt, va_list args) num =3D va_arg(args, unsigned int); } =20 - str =3D number(str, end, num, spec); + number(out, num, spec); } } - out: - if (size > 0) { - if (str < end) - *str =3D '\0'; - else - end[-1] =3D '\0'; - } + printbuf_nul_terminate(out); +} +EXPORT_SYMBOL(vpr_buf); =20 - /* the trailing null byte doesn't count towards the total */ - return str-buf; +/** + * pr_buf - Format a string, outputting to a printbuf + * @out: The printbuf to output to + * @fmt: The format string to use + * @args: Arguments for the format string + * + * + * pr_buf works much like the traditional sprintf(), but outputs to a + * printbuf instead of raw pointer/size. + * + * See the vsnprintf() documentation for format string extensions over C99. + */ +void pr_buf(struct printbuf *out, const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + vpr_buf(out, fmt, args); + va_end(args); +} +EXPORT_SYMBOL(pr_buf); + +/** + * vsnprintf - Format a string and place it in a buffer + * @buf: The buffer to place the result into + * @size: The size of the buffer, including the trailing null space + * @fmt: The format string to use + * @args: Arguments for the format string + * + * This function generally follows C99 vsnprintf, but has some + * extensions and a few limitations: + * + * - ``%n`` is unsupported + * - ``%p*`` is handled by pointer() + * + * See pointer() or Documentation/core-api/printk-formats.rst for more + * extensive description. + * + * **Please update the documentation in both places when making changes** + * + * The return value is the number of characters which would + * be generated for the given input, excluding the trailing + * '\0', as per ISO C99. If you want to have the exact + * number of characters written into @buf as return value + * (not including the trailing '\0'), use vscnprintf(). If the + * return is greater than or equal to @size, the resulting + * string is truncated. + * + * If you're not already dealing with a va_list consider using snprintf(). + */ +int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) +{ + struct printbuf out =3D PRINTBUF_EXTERN(buf, size); =20 + vpr_buf(&out, fmt, args); + + return out.pos; } EXPORT_SYMBOL(vsnprintf); =20 @@ -3008,6 +2911,13 @@ EXPORT_SYMBOL(sprintf); * bstr_printf() - Binary data to text string */ =20 +static inline void printbuf_align(struct printbuf *out, unsigned align) +{ + /* Assumes output buffer is correctly aligned: */ + out->pos +=3D align - 1; + out->pos &=3D ~(align - 1); +} + /** * vbin_printf - Parse a format string and place args' binary value in a b= uffer * @bin_buf: The buffer to place args' binary value @@ -3027,34 +2937,25 @@ EXPORT_SYMBOL(sprintf); */ int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) { + struct printbuf out =3D PRINTBUF_EXTERN((char *) bin_buf, size); struct printf_spec spec =3D {0}; - char *str, *end; int width; =20 - str =3D (char *)bin_buf; - end =3D (char *)(bin_buf + size); - #define save_arg(type) \ ({ \ unsigned long long value; \ if (sizeof(type) =3D=3D 8) { \ - unsigned long long val8; \ - str =3D PTR_ALIGN(str, sizeof(u32)); \ - val8 =3D va_arg(args, unsigned long long); \ - if (str + sizeof(type) <=3D end) { \ - *(u32 *)str =3D *(u32 *)&val8; \ - *(u32 *)(str + 4) =3D *((u32 *)&val8 + 1); \ - } \ + u64 val8 =3D va_arg(args, u64); \ + printbuf_align(&out, sizeof(u32)); \ + pr_bytes(&out, (u32 *) &val8, 4); \ + pr_bytes(&out, ((u32 *) &val8) + 1, 4); \ value =3D val8; \ } else { \ - unsigned int val4; \ - str =3D PTR_ALIGN(str, sizeof(type)); \ - val4 =3D va_arg(args, int); \ - if (str + sizeof(type) <=3D end) \ - *(typeof(type) *)str =3D (type)(long)val4; \ + u32 val4 =3D va_arg(args, u32); \ + printbuf_align(&out, sizeof(type)); \ + pr_bytes(&out, &val4, sizeof(type)); \ value =3D (unsigned long long)val4; \ } \ - str +=3D sizeof(type); \ value; \ }) =20 @@ -3085,16 +2986,12 @@ int vbin_printf(u32 *bin_buf, size_t size, const ch= ar *fmt, va_list args) case FORMAT_TYPE_STR: { const char *save_str =3D va_arg(args, char *); const char *err_msg; - size_t len; =20 err_msg =3D check_pointer_msg(save_str); if (err_msg) save_str =3D err_msg; =20 - len =3D strlen(save_str) + 1; - if (str + len < end) - memcpy(str, save_str, len); - str +=3D len; + pr_str(&out, save_str); break; } =20 @@ -3114,12 +3011,7 @@ int vbin_printf(u32 *bin_buf, size_t size, const cha= r *fmt, va_list args) save_arg(void *); break; } - str =3D pointer(fmt, str, end, va_arg(args, void *), - spec); - if (str + 1 < end) - *str++ =3D '\0'; - else - end[-1] =3D '\0'; /* Must be nul terminated */ + pointer(&out, fmt, va_arg(args, void *), spec); } /* skip all alphanumeric pointer suffixes */ while (isalnum(*fmt)) @@ -3157,7 +3049,9 @@ int vbin_printf(u32 *bin_buf, size_t size, const char= *fmt, va_list args) } =20 out: - return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; + printbuf_nul_terminate(&out); + printbuf_align(&out, 4); + return out.pos; #undef save_arg } EXPORT_SYMBOL_GPL(vbin_printf); @@ -3187,14 +3081,15 @@ EXPORT_SYMBOL_GPL(vbin_printf); int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_bu= f) { struct printf_spec spec =3D {0}; - char *str, *end; + struct printbuf out =3D PRINTBUF_EXTERN((char *) bin_buf, size); const char *args =3D (const char *)bin_buf; =20 if (WARN_ON_ONCE(size > INT_MAX)) return 0; =20 - str =3D buf; - end =3D buf + size; + /* Make sure end is always >=3D buf */ + if (WARN_ON_ONCE(out.buf + out.size < out.buf)) + return 0; =20 #define get_arg(type) \ ({ \ @@ -3211,12 +3106,6 @@ int bstr_printf(char *buf, size_t size, const char *= fmt, const u32 *bin_buf) value; \ }) =20 - /* Make sure end is always >=3D buf */ - if (end < buf) { - end =3D ((void *)-1); - size =3D end - buf; - } - while (*fmt) { const char *old_fmt =3D fmt; int read =3D format_decode(fmt, &spec); @@ -3224,16 +3113,9 @@ int bstr_printf(char *buf, size_t size, const char *= fmt, const u32 *bin_buf) fmt +=3D read; =20 switch (spec.type) { - case FORMAT_TYPE_NONE: { - int copy =3D read; - if (str < end) { - if (copy > end - str) - copy =3D end - str; - memcpy(str, old_fmt, copy); - } - str +=3D read; + case FORMAT_TYPE_NONE: + pr_bytes(&out, old_fmt, read); break; - } =20 case FORMAT_TYPE_WIDTH: set_field_width(&spec, get_arg(int)); @@ -3243,38 +3125,24 @@ int bstr_printf(char *buf, size_t size, const char = *fmt, const u32 *bin_buf) set_precision(&spec, get_arg(int)); break; =20 - case FORMAT_TYPE_CHAR: { - char c; - - if (!(spec.flags & LEFT)) { - while (--spec.field_width > 0) { - if (str < end) - *str =3D ' '; - ++str; - } - } - c =3D (unsigned char) get_arg(char); - if (str < end) - *str =3D c; - ++str; - while (--spec.field_width > 0) { - if (str < end) - *str =3D ' '; - ++str; - } + case FORMAT_TYPE_CHAR: + if (!(spec.flags & LEFT)) + pr_chars(&out, spec.field_width, ' '); + __pr_char(&out, (unsigned char) get_arg(char)); + if ((spec.flags & LEFT)) + pr_chars(&out, spec.field_width, ' '); break; - } =20 case FORMAT_TYPE_STR: { const char *str_arg =3D args; args +=3D strlen(str_arg) + 1; - str =3D string(str, end, (char *)str_arg, spec); + string(&out, (char *)str_arg, spec); break; } =20 case FORMAT_TYPE_PTR: { bool process =3D false; - int copy, len; + int len; /* Non function dereferences were already done */ switch (*fmt) { case 'S': @@ -3290,17 +3158,12 @@ int bstr_printf(char *buf, size_t size, const char = *fmt, const u32 *bin_buf) break; } /* Pointer dereference was already processed */ - if (str < end) { - len =3D copy =3D strlen(args); - if (copy > end - str) - copy =3D end - str; - memcpy(str, args, copy); - str +=3D len; - args +=3D len + 1; - } + len =3D strlen(args); + pr_bytes(&out, args, len); + args +=3D len + 1; } if (process) - str =3D pointer(fmt, str, end, get_arg(void *), spec); + pointer(&out, fmt, get_arg(void *), spec); =20 while (isalnum(*fmt)) fmt++; @@ -3308,9 +3171,7 @@ int bstr_printf(char *buf, size_t size, const char *f= mt, const u32 *bin_buf) } =20 case FORMAT_TYPE_PERCENT_CHAR: - if (str < end) - *str =3D '%'; - ++str; + __pr_char(&out, '%'); break; =20 case FORMAT_TYPE_INVALID: @@ -3353,23 +3214,15 @@ int bstr_printf(char *buf, size_t size, const char = *fmt, const u32 *bin_buf) num =3D get_arg(int); } =20 - str =3D number(str, end, num, spec); + number(&out, num, spec); } /* default: */ } /* switch(spec.type) */ } /* while(*fmt) */ =20 out: - if (size > 0) { - if (str < end) - *str =3D '\0'; - else - end[-1] =3D '\0'; - } - #undef get_arg - - /* the trailing null byte doesn't count towards the total */ - return str - buf; + printbuf_nul_terminate(&out); + return out.pos; } EXPORT_SYMBOL_GPL(bstr_printf); =20 --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A023CC433F5 for ; Thu, 19 May 2022 17:25:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241281AbiESRZF (ORCPT ); Thu, 19 May 2022 13:25:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242582AbiESRYm (ORCPT ); Thu, 19 May 2022 13:24:42 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0FDE3A5036; Thu, 19 May 2022 10:24:38 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id bh5so5376893plb.6; Thu, 19 May 2022 10:24:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KezD3kfL2rJaxz73jmeIdmWUTP5JswkB1sqSfdCS7Nk=; b=Sv4CpsY5gZOPnxw2hhkBifiwf/Z9F6vWxInFppvcO4lgxcigcYj6nCMSIcs4BWPZh0 wijlPWIeLY1qKHN9EYWbG5fFhlOZ+llQRVNikmUb7crMDg/YnHGUmSuQKRksQb1LIRvR km7qLFcltoy5T2JtCR5vXcF6ii3S59rYsILQnEC7fWZCWnWV40Lw3CaxjZnt5GqeNiXH BYDJlVkdyo122/rK+XpYlvj/nasdfE12T7zRa39WBh3fDZFhZ5nGDg+kEoanICGs5nPH 0Uv16gcGHTrrVaVhhA0U0QV1/wdctSpFdCRiDaocSYF/5CHVvTWdI2GgbD9t6OY48MdG rd9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KezD3kfL2rJaxz73jmeIdmWUTP5JswkB1sqSfdCS7Nk=; b=1PqxSgXfEawSaqc7R/80Ead+akH4/vrIC9r6/5wo9J05oWIPw36bEKTl0afrsw0i8r cIxBdhiOr60U+zhkVjYUJGsun2fGePYKF0KHAieFvyE+GrWPJWepu4AU7OB3BcIbAnKY NaFwPu3SAsrw+yH6FfeaSMK0fR+jX6d4bQxKF/RmWS0U7S0xyFsR5DAUgMzXzmu8S88W Zv3RH/EDoRpZfOkoptsFCpfNx1hDo2aLEoFBEimAH+uAE14ng0IR4jXj7/sojMzj79l/ HaPmHEnNUr2KZbTGrfMtoGF9K6lxeVyxBeaz2WL4Nhf/iIn+U3yGNPI3u2GOQdtTc+rX a5kg== X-Gm-Message-State: AOAM532suEbE2DF2B0llDHzbr59LGgd0F/B4o4gSOka+nxCkCey4KuEa HWagj6AK8ZZ7h9u5WK2IrBOUHOQ04HJQ X-Google-Smtp-Source: ABdhPJxnqTf8vOeejJX87OBSIDtc6xFKWDKNsZsXQBir9VBe+WezI5BtPcdzvylFmNpsvnf7l1KQuw== X-Received: by 2002:a17:902:9a83:b0:15e:f718:4ced with SMTP id w3-20020a1709029a8300b0015ef7184cedmr5416529plp.69.1652981076974; Thu, 19 May 2022 10:24:36 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.24.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:24:36 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 03/28] vsprintf: %pf(%p) Date: Thu, 19 May 2022 13:23:56 -0400 Message-Id: <20220519172421.162394-4-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This implements two new format strings: both do the same thing, one more compatible with current gcc format string checking, the other that we'd like to standardize: %p(%p) - more compatible %(%p) - more prettier Both can take variable numbers of arguments, i.e. %(%p,%p,%p). They're used to indicate that snprintf or pr_buf should interpret the next argument as a pretty-printer function to call, and subsequent arguments within the parentheses should be passed to the pretty-printer. A pretty printer takes as its first argument a printbuf, and then zero or more pointer arguments - integer arguments are not (currently) supported. Example usage: static void foo_to_text(struct printbuf *out, struct foo *foo) { pr_buf(out, "bar=3D%u baz=3D%u", foo->bar, foo->baz); } printf("%(%p)", foo_to_text, foo); The goal is to replace most of our %p format extensions with this interface, and to move pretty-printers out of the core vsprintf.c code - this will get us better organization and better discoverability (you'll be able to cscope to pretty printer calls!), as well as eliminate a lot of dispatch code in vsprintf.c. Currently, we can only call pretty printers with pointer arguments. This could be changed to also allow at least integer arguments in the future by using libffi. Signed-off-by: Kent Overstreet Reviewed-by: Matthew Wilcox (Oracle) --- Documentation/core-api/printk-formats.rst | 19 ++++++ lib/test_printf.c | 17 +++++ lib/vsprintf.c | 81 ++++++++++++++++++++++- 3 files changed, 115 insertions(+), 2 deletions(-) diff --git a/Documentation/core-api/printk-formats.rst b/Documentation/core= -api/printk-formats.rst index 5e89497ba3..e4ed40960e 100644 --- a/Documentation/core-api/printk-formats.rst +++ b/Documentation/core-api/printk-formats.rst @@ -625,6 +625,25 @@ Examples:: %p4cc Y10 little-endian (0x20303159) %p4cc NV12 big-endian (0xb231564e) =20 +Calling a pretty printer function +--------------------------------- + +:: + + %p(%p) pretty printer function taking one argument + %p(%p,%p) pretty printer function taking two arguments + +For calling generic pretty printers. A pretty printer is a function that t= akes +as its first argument a pointer to a printbuf, and then zero or more addit= ional +pointer arguments. For example: + + void foo_to_text(struct printbuf *out, struct foo *foo) + { + pr_buf(out, "bar=3D%u baz=3D%u", foo->bar, foo->baz); + } + + printf("%p(%p)", foo_to_text, foo); + Thanks =3D=3D=3D=3D=3D=3D =20 diff --git a/lib/test_printf.c b/lib/test_printf.c index 07309c45f3..1e1604ef1a 100644 --- a/lib/test_printf.c +++ b/lib/test_printf.c @@ -783,6 +783,22 @@ test_pointer(void) fourcc_pointer(); } =20 +static void printf_test_fn(struct printbuf *out, void *p) +{ + int *i =3D p; + + pr_buf(out, "%i", *i); +} + +static void __init +test_fn(void) +{ + int i =3D 1; + + test("1", "%pf(%p)", printf_test_fn, &i); + test("1", "%(%p)", printf_test_fn, &i); +} + static void __init selftest(void) { alloced_buffer =3D kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL); @@ -794,6 +810,7 @@ static void __init selftest(void) test_number(); test_string(); test_pointer(); + test_fn(); =20 kfree(alloced_buffer); } diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 09b259e030..7fbeaf50d1 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -431,7 +431,8 @@ enum format_type { FORMAT_TYPE_UINT, FORMAT_TYPE_INT, FORMAT_TYPE_SIZE_T, - FORMAT_TYPE_PTRDIFF + FORMAT_TYPE_PTRDIFF, + FORMAT_TYPE_FN, }; =20 struct printf_spec { @@ -2512,7 +2513,16 @@ int format_decode(const char *fmt, struct printf_spe= c *spec) return ++fmt - start; =20 case 'p': - spec->type =3D FORMAT_TYPE_PTR; + fmt++; + if (fmt[0] =3D=3D 'f' && + fmt[1] =3D=3D '(') { + fmt +=3D 2; + spec->type =3D FORMAT_TYPE_FN; + } else + spec->type =3D FORMAT_TYPE_PTR; + return fmt - start; + case '(': + spec->type =3D FORMAT_TYPE_FN; return ++fmt - start; =20 case '%': @@ -2594,6 +2604,49 @@ set_precision(struct printf_spec *spec, int prec) } } =20 +static void call_pr_fn(struct printbuf *out, void *fn, void **fn_args, uns= igned nr_args) +{ + typedef void (*printf_fn_0)(struct printbuf *); + typedef void (*printf_fn_1)(struct printbuf *, void *); + typedef void (*printf_fn_2)(struct printbuf *, void *, void *); + typedef void (*printf_fn_3)(struct printbuf *, void *, void *, void *); + typedef void (*printf_fn_4)(struct printbuf *, void *, void *, void *, vo= id *); + typedef void (*printf_fn_5)(struct printbuf *, void *, void *, void *, vo= id *, void *); + typedef void (*printf_fn_6)(struct printbuf *, void *, void *, void *, vo= id *, void *, void *); + typedef void (*printf_fn_7)(struct printbuf *, void *, void *, void *, vo= id *, void *, void *, void *); + typedef void (*printf_fn_8)(struct printbuf *, void *, void *, void *, vo= id *, void *, void *, void *, void *); + + switch (nr_args) { + case 0: + ((printf_fn_0)fn)(out); + break; + case 1: + ((printf_fn_1)fn)(out, fn_args[0]); + break; + case 2: + ((printf_fn_2)fn)(out, fn_args[0], fn_args[1]); + break; + case 3: + ((printf_fn_3)fn)(out, fn_args[0], fn_args[1], fn_args[2]); + break; + case 4: + ((printf_fn_4)fn)(out, fn_args[0], fn_args[1], fn_args[2], fn_args[3]); + break; + case 5: + ((printf_fn_5)fn)(out, fn_args[0], fn_args[1], fn_args[2], fn_args[3], f= n_args[4]); + break; + case 6: + ((printf_fn_6)fn)(out, fn_args[0], fn_args[1], fn_args[2], fn_args[3], f= n_args[4], fn_args[5]); + break; + case 7: + ((printf_fn_7)fn)(out, fn_args[0], fn_args[1], fn_args[2], fn_args[3], f= n_args[4], fn_args[5], fn_args[6]); + break; + case 8: + ((printf_fn_8)fn)(out, fn_args[0], fn_args[1], fn_args[2], fn_args[3], f= n_args[4], fn_args[5], fn_args[6], fn_args[7]); + break; + } +} + /** * vpr_buf - Format a string, outputting to a printbuf * @out: The printbuf to output to @@ -2657,6 +2710,30 @@ void vpr_buf(struct printbuf *out, const char *fmt, = va_list args) fmt++; break; =20 + case FORMAT_TYPE_FN: { + unsigned nr_args =3D 0; + void *fn_args[8]; + void *fn =3D va_arg(args, void *); + + while (1) { + if (WARN_ON_ONCE(nr_args =3D=3D ARRAY_SIZE(fn_args))) + goto out; + if (*fmt++ !=3D '%') + goto out; + if (*fmt++ !=3D 'p') + goto out; + fn_args[nr_args++] =3D va_arg(args, void *); + if (*fmt =3D=3D ')') + break; + if (*fmt++ !=3D ',') + goto out; + } + + call_pr_fn(out, fn, fn_args, nr_args); + fmt++; /* past trailing ) */ + break; + } + case FORMAT_TYPE_PERCENT_CHAR: __pr_char(out, '%'); break; --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 529C1C433EF for ; Thu, 19 May 2022 17:25:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242665AbiESRZO (ORCPT ); Thu, 19 May 2022 13:25:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242589AbiESRYm (ORCPT ); Thu, 19 May 2022 13:24:42 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 084CFA503B; Thu, 19 May 2022 10:24:40 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id y41so5691020pfw.12; Thu, 19 May 2022 10:24:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Aez4xKkQFiC6cLQoLCSDnU78rKX2tZ7O2uihdwY+gWM=; b=a1gmaGVsBWgj8W0Ucn3OWFhvF/01NImYP50L+JkvTxNU6+V+3rp20qVM+mTtTCKDMz FAc2Gnd2oFzwp5hWXQsMc0dXwFVmpA2UsaxqzWluk6793qpF9tsHKxb2N+CGyl9lyugt zuQKBLCSXPuii9QMdz7DhJdlvtwgCVKl7ADO63ZV3gQg9RKqOKy+qIsjlLgv7KHisQ5O 4/Doyf0JlPj0/6hnUHB/3a3de+JgshjNQskdJAmVSX3LEx3WAbLBiirB6QD/rJaU62/k MQ/UQNFPcBLz8b27gJmRNMiDUVFdsG03lZgNSvlnU1FQ/wIm+L1XCEsQd7jpS7ado8Nm QqwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Aez4xKkQFiC6cLQoLCSDnU78rKX2tZ7O2uihdwY+gWM=; b=3ymAPrpmeIEPb6JlZbtr0BuHd3FG2bAuKYs/jgxD2bTh+NKABXP9rnA4pCiKZR873p LYgTNtVpserv9Dcq5/y5U3OhnjWXNW2dMDtcHAFEjbVnALxuhjFuiVD/AYU3MuA0goYx 3ZqGV4sPVfKUXJcCoxFZPxQelVWKz1wX8drCMGr3i+jyZXYbvrvvj1H6qkGINpxAjAZE ffMYH0vk4fdizIDWP58Jkmh/dg+FKY98MuXZc/43iiGpqLt9+0ru/ZUXByPrkX181sB+ 0dCr7vSSmr3gBJNYb/M08B2JqDeVutqc1zSaVzJ3jf5Zv4ogvoOxh0YfIcaNupEU6UjP skGw== X-Gm-Message-State: AOAM531cpJlUD/7mlyfmKUnh2SqZKhQ48utrJcDmV/at3H2TkEyFRTvL 68tBPq5sjF1MxPpwDjgXqk6tRvEClAfl X-Google-Smtp-Source: ABdhPJyDTVqemec481fu2a98YNEPDxW1lSMzFlRcj5H/c1FC/s0T9u9nb+ujrC195v2JIu/E2GYAmQ== X-Received: by 2002:a05:6a00:234f:b0:4f6:f0c0:ec68 with SMTP id j15-20020a056a00234f00b004f6f0c0ec68mr5742305pfj.14.1652981079196; Thu, 19 May 2022 10:24:39 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.24.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:24:38 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 04/28] lib/string_helpers: string_get_size() now returns characters wrote Date: Thu, 19 May 2022 13:23:57 -0400 Message-Id: <20220519172421.162394-5-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" printbuf now needs to know the number of characters that would have been written if the buffer was too small, like snprintf(); this changes string_get_size() to return the the return value of snprintf(). Signed-off-by: Kent Overstreet --- include/linux/string_helpers.h | 4 ++-- lib/string_helpers.c | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h index 2e56b4ce98..fd9c150a12 100644 --- a/include/linux/string_helpers.h +++ b/include/linux/string_helpers.h @@ -19,8 +19,8 @@ enum string_size_units { STRING_UNITS_2, /* use binary powers of 2^10 */ }; =20 -void string_get_size(u64 size, u64 blk_size, enum string_size_units units, - char *buf, int len); +int string_get_size(u64 size, u64 blk_size, enum string_size_units units, + char *buf, int len); =20 #define UNESCAPE_SPACE BIT(0) #define UNESCAPE_OCTAL BIT(1) diff --git a/lib/string_helpers.c b/lib/string_helpers.c index c890e83f59..9a08b5d774 100644 --- a/lib/string_helpers.c +++ b/lib/string_helpers.c @@ -33,8 +33,8 @@ * at least 9 bytes and will always be zero terminated. * */ -void string_get_size(u64 size, u64 blk_size, const enum string_size_units = units, - char *buf, int len) +int string_get_size(u64 size, u64 blk_size, const enum string_size_units u= nits, + char *buf, int len) { static const char *const units_10[] =3D { "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" @@ -127,8 +127,7 @@ void string_get_size(u64 size, u64 blk_size, const enum= string_size_units units, else unit =3D units_str[units][i]; =20 - snprintf(buf, len, "%u%s %s", (u32)size, - tmp, unit); + return snprintf(buf, len, "%u%s %s", (u32)size, tmp, unit); } EXPORT_SYMBOL(string_get_size); =20 --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E3FCBC433EF for ; Thu, 19 May 2022 17:25:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242653AbiESRZ1 (ORCPT ); Thu, 19 May 2022 13:25:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240203AbiESRYo (ORCPT ); Thu, 19 May 2022 13:24:44 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 684BDA2041; Thu, 19 May 2022 10:24:42 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id i24so5706065pfa.7; Thu, 19 May 2022 10:24:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=kqqe897jT3bJTlDV5KkrLu5Sm6S8Vcy+zi9IaoKFPgU=; b=q5C7u/12y0rwLUACTBBnOlgcMundJ8zp0NHyYcrZnSqhptgjv81mpLjAFr0GK2y65A BMFSP4pIeB7mb0FkFsXJQY4BFjrcXqU9hy7URpYv6OQt1//ponh3vwmM6n/nZl5srl55 uNsA/a3s132XRx6qc4lz63K7E3Jg99VSd7uklS+mScoZQIDDX1YJeXtRPwj/bAA6CWRg EC91oFj8Yiav1+aVPayDHqPr9vG5VEBpkbpY6UDV95Njqdb9M/UJxhvnuxkZJpgZ61wU qo3Pxj6pT5G4u+c8+uCUlzKGUOUtpQUGWbW9lEadxmZalkHcA+faU6u8bgeZKmqC6i3Y kWQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=kqqe897jT3bJTlDV5KkrLu5Sm6S8Vcy+zi9IaoKFPgU=; b=SxJPmauPNJc5GhkIbeJ0Eo517bQcVaTbJHhU0C7j5N9Q0Mpfcx2KrOU17wRo8oIuab q7kBEcmKWOMc8GqBgYivHKGW+U5X1JKxnC/s+1zCEexxy+7p+MZEBZsDzKRYPyTAakSe NK4c0EYyBZ617twtzXDhkfl+Zd+Kb/llGBJzXSHJwbMjRmI83W4s2rYCf3EsmvjtPM4L LIuXM8Yi1k1IihjtMlFqjBqyaLveKJoEgv9IMe7ZLcLRfazO5+6/BLlnWN7g5ctTj0TW REVgPcBNmvgIHbkXN+h1petuzekn0LtGmmF2k6lhGpfT/W1GDHdnqMD6Pp5P8WO83att hhZQ== X-Gm-Message-State: AOAM532d5VMGVeW4Ne5ALHMgU3ammzzPPBeZSWg5pi0zHYqPQrh+49zD 4KvdOiJmiNrPZbop5Tkm91g8V4S+koRX X-Google-Smtp-Source: ABdhPJwFScg0uiMUswmKY0DiYEJDr0W8KYuCisAy4+GQKy9hveECuABrXMRKNUvHLMMyviOHUAXPTg== X-Received: by 2002:a65:6051:0:b0:39d:1b00:e473 with SMTP id a17-20020a656051000000b0039d1b00e473mr4779741pgp.578.1652981081345; Thu, 19 May 2022 10:24:41 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.24.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:24:40 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 05/28] lib/printbuf: Heap allocation Date: Thu, 19 May 2022 13:23:58 -0400 Message-Id: <20220519172421.162394-6-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This makes printbufs optionally heap allocated: a printbuf initialized with the PRINTBUF initializer will automatically heap allocate and resize as needed. Allocations are done with GFP_KERNEL: code should use e.g. memalloc_nofs_save()/restore() as needed. Since we do not currently have memalloc_nowait_save()/restore(), in contexts where it is not safe to block we provide the helpers printbuf_atomic_inc() printbuf_atomic_dec() When the atomic count is nonzero, memory allocations will be done with GFP_NOWAIT. On memory allocation failure, output will be truncated. Code that wishes to check for memory allocation failure (in contexts where we should return -ENOMEM) should check if printbuf->allocation_failure is set. Since printbufs are expected to be typically used for log messages and on a best effort basis, we don't return errors directly. Other helpers provided by this patch: - printbuf_make_room(buf, extra) Reallocates if necessary to make room for @extra bytes (not including terminating null). - printbuf_str(buf) Returns a null terminated string equivalent to the contents of @buf. If @buf was never allocated (or allocation failed), returns a constant empty string. - printbuf_exit(buf) Releases memory allocated by a printbuf. Signed-off-by: Kent Overstreet --- include/linux/printbuf.h | 118 ++++++++++++++++++++++++++++++++++----- lib/Makefile | 2 +- lib/printbuf.c | 70 +++++++++++++++++++++++ 3 files changed, 174 insertions(+), 16 deletions(-) create mode 100644 lib/printbuf.c diff --git a/include/linux/printbuf.h b/include/linux/printbuf.h index 40dc07040d..c114072773 100644 --- a/include/linux/printbuf.h +++ b/include/linux/printbuf.h @@ -4,18 +4,68 @@ #ifndef _LINUX_PRINTBUF_H #define _LINUX_PRINTBUF_H =20 -#include - /* - * 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 c= ode + * portable between kernelspace and userspace. + * + * Basic example: + * struct printbuf buf =3D PRINTBUF; + * + * pr_buf(&buf, "foo=3D"); + * foo_to_text(&buf, foo); + * printk("%s", buf.buf); + * printbuf_exit(&buf); + * + * Or + * struct printbuf buf =3D 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 alloca= tions + * 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 doe= s want + * to return -ENOMEM may check printbuf.allocation_failure. */ =20 +#include + 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; }; =20 +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 =3D true }) + +/* Initializer a printbuf that points to an external buffer: */ +#define PRINTBUF_EXTERN(_buf, _size) \ +((struct printbuf) { \ + .buf =3D _buf, \ + .size =3D _size, \ +}) + static inline unsigned printbuf_remaining(struct printbuf *out) { return out->pos < out->size ? out->size - out->pos : 0; @@ -28,28 +78,44 @@ static inline unsigned printbuf_written(struct printbuf= *out) =20 static inline void printbuf_nul_terminate(struct printbuf *out) { + printbuf_make_room(out, 1); + if (out->pos < out->size) out->buf[out->pos] =3D 0; else if (out->size) out->buf[out->size - 1] =3D 0; } =20 -static inline void pr_chars(struct printbuf *out, char c, unsigned n) +static inline void __pr_chars_reserved(struct printbuf *out, char c, unsig= ned n) { memset(out->buf + out->pos, c, min(n, printbuf_remaining(out))); out->pos +=3D n; +} + +static inline void pr_chars(struct printbuf *out, char c, unsigned n) +{ + printbuf_make_room(out, n); + __pr_chars_reserved(out, c, n); printbuf_nul_terminate(out); } =20 -static inline void __pr_char(struct printbuf *out, char c) +/* Doesn't call printbuf_make_room(), doesn't nul terminate: */ +static inline void __pr_char_reserved(struct printbuf *out, char c) { if (printbuf_remaining(out)) out->buf[out->pos] =3D c; out->pos++; } =20 +/* Doesn't nul terminate: */ +static inline void __pr_char(struct printbuf *out, char c) +{ + printbuf_make_room(out, 1); + __pr_char_reserved(out, c); +} + static inline void pr_char(struct printbuf *out, char c) { __pr_char(out, c); @@ -58,6 +124,8 @@ static inline void pr_char(struct printbuf *out, char c) =20 static inline void pr_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))); @@ -72,23 +140,43 @@ static inline void pr_str(struct printbuf *out, const = char *str) =20 static inline void pr_hex_byte(struct printbuf *out, u8 byte) { - __pr_char(out, hex_asc_hi(byte)); - __pr_char(out, hex_asc_lo(byte)); + printbuf_make_room(out, 2); + __pr_char_reserved(out, hex_asc_hi(byte)); + __pr_char_reserved(out, hex_asc_lo(byte)); printbuf_nul_terminate(out); } =20 static inline void pr_hex_byte_upper(struct printbuf *out, u8 byte) { - __pr_char(out, hex_asc_upper_hi(byte)); - __pr_char(out, hex_asc_upper_lo(byte)); + printbuf_make_room(out, 2); + __pr_char_reserved(out, hex_asc_upper_hi(byte)); + __pr_char_reserved(out, hex_asc_upper_lo(byte)); printbuf_nul_terminate(out); } =20 -#define PRINTBUF ((struct printbuf) { .si_units =3D PRINTBUF_UNITS_10 }) -#define PRINTBUF_EXTERN(_buf, _size) \ -((struct printbuf) { \ - .buf =3D _buf, \ - .size =3D _size, \ -}) +/** + * printbuf_reset - re-use a printbuf without freeing and re-initializing = it: + */ +static inline void printbuf_reset(struct printbuf *buf) +{ + buf->pos =3D 0; + buf->allocation_failure =3D 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--; +} =20 #endif /* _LINUX_PRINTBUF_H */ diff --git a/lib/Makefile b/lib/Makefile index c588a126a3..31a3904eda 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -34,7 +34,7 @@ lib-y :=3D ctype.o string.o vsprintf.o cmdline.o \ is_single_threaded.o plist.o decompress.o kobject_uevent.o \ earlycpio.o seq_buf.o siphash.o dec_and_lock.o \ nmi_backtrace.o nodemask.o win_minmax.o memcat_p.o \ - buildid.o + buildid.o printbuf.o =20 lib-$(CONFIG_PRINTK) +=3D dump_stack.o lib-$(CONFIG_SMP) +=3D cpumask.o diff --git a/lib/printbuf.c b/lib/printbuf.c new file mode 100644 index 0000000000..0093b34158 --- /dev/null +++ b/lib/printbuf.c @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: LGPL-2.1+ +/* Copyright (C) 2022 Kent Overstreet */ + +#ifdef __KERNEL__ +#include +#include +#else +#define EXPORT_SYMBOL(x) +#endif + +#include +#include + +int printbuf_make_room(struct printbuf *out, unsigned extra) +{ + unsigned new_size; + char *buf; + + if (!out->heap_allocated) + return 0; + + /* Reserved space for terminating nul: */ + extra +=3D 1; + + if (out->pos + extra < out->size) + return 0; + + new_size =3D roundup_pow_of_two(out->size + extra); + buf =3D krealloc(out->buf, new_size, !out->atomic ? GFP_KERNEL : GFP_NOWA= IT); + + if (!buf) { + out->allocation_failure =3D true; + return -ENOMEM; + } + + out->buf =3D buf; + out->size =3D new_size; + return 0; +} +EXPORT_SYMBOL(printbuf_make_room); + +/** + * printbuf_str - returns printbuf's buf as a C string, guaranteed to be n= ull + * terminated + */ +const char *printbuf_str(const struct printbuf *buf) +{ + /* + * If we've written to a printbuf then it's guaranteed to be a null + * terminated string - but if we haven't, then we might not have + * allocated a buffer at all: + */ + return buf->pos + ? buf->buf + : ""; +} +EXPORT_SYMBOL(printbuf_str); + +/** + * printbuf_exit - exit a printbuf, freeing memory it owns and poisoning it + * against accidental use. + */ +void printbuf_exit(struct printbuf *buf) +{ + if (buf->heap_allocated) { + kfree(buf->buf); + buf->buf =3D ERR_PTR(-EINTR); /* poison value */ + } +} +EXPORT_SYMBOL(printbuf_exit); --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47A37C433EF for ; Thu, 19 May 2022 17:25:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242737AbiESRZf (ORCPT ); Thu, 19 May 2022 13:25:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242577AbiESRYs (ORCPT ); Thu, 19 May 2022 13:24:48 -0400 Received: from mail-pg1-x534.google.com (mail-pg1-x534.google.com [IPv6:2607:f8b0:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE4ADA2041; Thu, 19 May 2022 10:24:46 -0700 (PDT) Received: by mail-pg1-x534.google.com with SMTP id a19so5650867pgw.6; Thu, 19 May 2022 10:24:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YdosP/pHOmdUT1GcMbPpmLyIQDuljI/X8gwrTiRDtk8=; b=CLWHKPizggH8N4nzmVjYeWABHQkwQQ4EZGGTG5p40MCenGmqMjOxhwW0j2bUEjl7h2 6JvN/ppxEcFG0ldzZaIFxgXZjZmtrKausESqomqKzNmIfiLlZa7vLeZEpQCUDUKkPLbK 7aoV3B1PI0WD6oMLuBAdypphAHs+xOWWcosq3KgxxDBszY4cqJ4Aeg6bfuQyPd278oWo 5bND+kUqBTL02OJuboTOn6uELv3dcEWpBTIcOc/9UmnXlfc0p0eURXnm3SxbiN9GE4PY E7ijHc5QeXLlhGM60LGFRljkxzJ5P5nJqVjdn38xoKaszI1aHHkxdCkkHjEFRwOUp53G MQ1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YdosP/pHOmdUT1GcMbPpmLyIQDuljI/X8gwrTiRDtk8=; b=enGvuIjgoKD19oyssyllF/m/rJLDphj3pDbOb9Ad3KAL2HROcdiFj1c47TNl5pFcug CC/1KCr1KdXXs+o9xP/kmq6QSyGfoai6uEkUT9kcAlIvIZO8K5roNe9cFPMqySV8E1/u PYImjlvNTFD5c3O2/q+ffX47WYT2Q+WWaGiyuKpxAA590tn9v4ik8Y3hMY1r0cOAt5wH /mQh+YsUEYzMbuVFK9NOBM9KNUV9miAD7sdDtCW01PEwekKIy9i4tUpA5Td6rLRrRg7T ZUUbInO2kFV1YLM0ow5aYGHvhY5mvnpVO72kmBxdUz3QOiNPoDrZHxoPHHKx0XUSzux4 uKMA== X-Gm-Message-State: AOAM533+xoZal/QIbpXodTiH1HOrgxV9nRCn3vmbJ80w76lEAr0XBJXY 9WbE239Y9oSd+4vB0bhfkPI7wbf+Q+E4 X-Google-Smtp-Source: ABdhPJzALpbDm1utpgUxPnAe87RywRmlshnN2xH9UbflTNbmf299iP48AK9D7ITuztAHQjPLL+ziEQ== X-Received: by 2002:a63:d743:0:b0:3f5:f77a:a516 with SMTP id w3-20020a63d743000000b003f5f77aa516mr4781472pgi.210.1652981085422; Thu, 19 May 2022 10:24:45 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.24.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:24:44 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 06/28] lib/printbuf: Tabstops, indenting Date: Thu, 19 May 2022 13:23:59 -0400 Message-Id: <20220519172421.162394-7-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This patch adds two new features to printbuf for structured formatting: - Indent level: the indent level, as a number of spaces, may be increased with pr_indent_add() and decreased with pr_indent_sub(). Subsequent lines, when started with pr_newline() (not "\n", although that may change) will then be intended according to the current indent level. This helps with pretty-printers that structure a large amonut of data across multiple lines and multiple functions. - Tabstops: Tabstops may be set by assigning to the printbuf->tabstops array. Then, pr_tab() may be used to advance to the next tabstop, printing as many spaces as required - leaving previous output left justified to the previous tabstop. pr_tab_rjust() advances to the next tabstop but inserts the spaces just after the previous tabstop - right justifying the previously-outputted text to the next tabstop. Signed-off-by: Kent Overstreet --- include/linux/printbuf.h | 28 +++++++++ lib/printbuf.c | 125 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) diff --git a/include/linux/printbuf.h b/include/linux/printbuf.h index c114072773..798e067457 100644 --- a/include/linux/printbuf.h +++ b/include/linux/printbuf.h @@ -36,6 +36,23 @@ * 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 doe= s want * to return -ENOMEM may check printbuf.allocation_failure. + * + * Indenting, tabstops: + * + * To aid is writing multi-line pretty printers spread across multiple + * functions, printbufs track the current indent level. + * + * pr_indent_push() and pr_indent_pop() increase and decrease the current = indent + * level, respectively. + * + * To use tabstops, set printbuf->tabstops[]; they are in units of spaces,= from + * start of line. Once set, pr_tab() will output spaces up to the next tab= stop. + * pr_tab_rjust() will also advance the current line of text up to the next + * tabstop, but it does so by shifting text since the previous tabstop up = to the + * next tabstop - right justifying it. + * + * Make sure you use pr_newline() instead of \n in the format string for i= ndent + * level and tabstops to work corretly. */ =20 #include @@ -44,18 +61,29 @@ struct printbuf { char *buf; unsigned size; unsigned pos; + unsigned last_newline; + unsigned last_field; + unsigned indent; /* * If nonzero, allocations will be done with GFP_ATOMIC: */ u8 atomic; bool allocation_failure:1; bool heap_allocated:1; + u8 tabstop; + u8 tabstops[4]; }; =20 int printbuf_make_room(struct printbuf *, unsigned); const char *printbuf_str(const struct printbuf *); void printbuf_exit(struct printbuf *); =20 +void pr_newline(struct printbuf *); +void pr_indent_add(struct printbuf *, unsigned); +void pr_indent_sub(struct printbuf *, unsigned); +void pr_tab(struct printbuf *); +void pr_tab_rjust(struct printbuf *); + /* Initializer for a heap allocated printbuf: */ #define PRINTBUF ((struct printbuf) { .heap_allocated =3D true }) =20 diff --git a/lib/printbuf.c b/lib/printbuf.c index 0093b34158..c9f730a215 100644 --- a/lib/printbuf.c +++ b/lib/printbuf.c @@ -11,6 +11,11 @@ #include #include =20 +static inline size_t printbuf_linelen(struct printbuf *buf) +{ + return buf->pos - buf->last_newline; +} + int printbuf_make_room(struct printbuf *out, unsigned extra) { unsigned new_size; @@ -68,3 +73,123 @@ void printbuf_exit(struct printbuf *buf) } } EXPORT_SYMBOL(printbuf_exit); + +void pr_newline(struct printbuf *buf) +{ + unsigned i; + + printbuf_make_room(buf, 1 + buf->indent); + + __pr_char(buf, '\n'); + + buf->last_newline =3D buf->pos; + + for (i =3D 0; i < buf->indent; i++) + __pr_char(buf, ' '); + + printbuf_nul_terminate(buf); + + buf->last_field =3D buf->pos; + buf->tabstop =3D 0; +} +EXPORT_SYMBOL(pr_newline); + +/** + * pr_indent_add - add to the current indent level + * + * @buf: printbuf to control + * @spaces: number of spaces to add to the current indent level + * + * Subsequent lines, and the current line if the output position is at the= start + * of the current line, will be indented by @spaces more spaces. + */ +void pr_indent_add(struct printbuf *buf, unsigned spaces) +{ + if (WARN_ON_ONCE(buf->indent + spaces < buf->indent)) + spaces =3D 0; + + buf->indent +=3D spaces; + while (spaces--) + pr_char(buf, ' '); +} +EXPORT_SYMBOL(pr_indent_add); + +/** + * pr_indent_sub - subtract from the current indent level + * + * @buf: printbuf to control + * @spaces: number of spaces to subtract from the current indent level + * + * Subsequent lines, and the current line if the output position is at the= start + * of the current line, will be indented by @spaces less spaces. + */ +void pr_indent_sub(struct printbuf *buf, unsigned spaces) +{ + if (WARN_ON_ONCE(spaces > buf->indent)) + spaces =3D buf->indent; + + if (buf->last_newline + buf->indent =3D=3D buf->pos) { + buf->pos -=3D spaces; + printbuf_nul_terminate(buf); + } + buf->indent -=3D spaces; +} +EXPORT_SYMBOL(pr_indent_sub); + +/** + * pr_tab - Advance printbuf to the next tabstop + * + * @buf: printbuf to control + * + * Advance output to the next tabstop by printing spaces. + */ +void pr_tab(struct printbuf *out) +{ + int spaces =3D max(0, out->tabstops[out->tabstop] - printbuf_linelen(out)= ); + + BUG_ON(out->tabstop > ARRAY_SIZE(out->tabstops)); + + pr_chars(out, ' ', spaces); + + out->last_field =3D out->pos; + out->tabstop++; +} +EXPORT_SYMBOL(pr_tab); + +/** + * pr_tab - Advance printbuf to the next tabstop, right justifying previous + * output + * + * @buf: printbuf to control + * + * Advance output to the next tabstop by inserting spaces immediately afte= r the + * previous tabstop, right justifying previously outputted text. + */ +void pr_tab_rjust(struct printbuf *buf) +{ + BUG_ON(buf->tabstop > ARRAY_SIZE(buf->tabstops)); + + if (printbuf_linelen(buf) < buf->tabstops[buf->tabstop]) { + unsigned move =3D buf->pos - buf->last_field; + unsigned shift =3D buf->tabstops[buf->tabstop] - + printbuf_linelen(buf); + + printbuf_make_room(buf, shift); + + if (buf->last_field + shift < buf->size) + memmove(buf->buf + buf->last_field + shift, + buf->buf + buf->last_field, + min(move, buf->size - 1 - buf->last_field - shift)); + + if (buf->last_field < buf->size) + memset(buf->buf + buf->last_field, ' ', + min(shift, buf->size - buf->last_field)); + + buf->pos +=3D shift; + printbuf_nul_terminate(buf); + } + + buf->last_field =3D buf->pos; + buf->tabstop++; +} +EXPORT_SYMBOL(pr_tab_rjust); --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F06EEC433EF for ; Thu, 19 May 2022 17:25:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242670AbiESRZj (ORCPT ); Thu, 19 May 2022 13:25:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242600AbiESRY4 (ORCPT ); Thu, 19 May 2022 13:24:56 -0400 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06FA6A5012; Thu, 19 May 2022 10:24:50 -0700 (PDT) Received: by mail-pl1-x62a.google.com with SMTP id 10so1947920plj.0; Thu, 19 May 2022 10:24:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=L8iwJ9KMm4n/JzWy8qd0BmKy4V9nQfOKn1lqPdviiLg=; b=KG4XwmRf0vMgoko6EfDoSBe0U5MdiIjKw2pDox2XLhBcvj/Wy9pBDz+XVt8+3oMz7m ilgB55zWZGMYdiPzzGWq0LWd72nQF1drhAbSAfmCdT8B+reV2pgUmp3psLc7DBnChiFy xUK96KUN1BJeHGj1o60k5gR1VyVqSlVHizf8PRSSayo26kX/ZJU9dEDtDdsgisRiUJ/k huedI3FiAfEZasJM/s0AttK/75a/F7QO1nJiV9ti9QkeCeDmtOuonUdjwIA10jpvgUY6 wfOigNeOiz69OTNnfYlC3RrL7xqqMWkjk7G6z9qaHA3+sN8CKFIm46MLwu9xmBhCnjmg zLwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=L8iwJ9KMm4n/JzWy8qd0BmKy4V9nQfOKn1lqPdviiLg=; b=WrLXhxh1mD8mrBIFovScZa4LA6Lbr4dmMNw1sDkvF1EpqrXyWlajs9W3cJuW/Dj6y4 QAA59SsyOPpLnPCCkAMVbKPIHOAf5dUFGHtZBQcYMWZgS2tozrZNQYzo5tTpcPYMqQNJ mq+hD4rV20pL9lQB6jS7meidFsMPMsm068so+4Nxx+TUbUSoiwun7E9tgGCHzzWUfM8w jTNYF58sqeIFUUQfP9okGesmFtCmlZY5TEKqJuHeVoTVZdhKGF09S9dZttdTGGtlPXEZ zOUAtxL2e8S7kLO8osKklKiURctd9dbXwOlszP48K1kau/4fG8Og9z+F/FVyWVjpAgm/ UrKw== X-Gm-Message-State: AOAM532mDRO1t9YdxWOrC0mlneGwly30X4OR+/WotyMzxoNLJ/+CXJ+s HJV1W9sgmmpgOdXDjjji2Jjqz4wlf5gF X-Google-Smtp-Source: ABdhPJwNAsXk8WrlQNmMUxOiakWa8HRVa3XKgG63gL+oghxcu3tQJZVMw0yfIaqOJOUvdI8yXemssA== X-Received: by 2002:a17:902:ea46:b0:15d:dbc:34f2 with SMTP id r6-20020a170902ea4600b0015d0dbc34f2mr5636922plg.60.1652981088676; Thu, 19 May 2022 10:24:48 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.24.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:24:47 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 07/28] lib/printbuf: Unit specifiers Date: Thu, 19 May 2022 13:24:00 -0400 Message-Id: <20220519172421.162394-8-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This adds options to printbuf for specifying whether units should be printed raw (default) or with human readable units, and for controlling whether human-readable units should be base 2 (default), or base 10. This also adds new helpers that obey these options: - pr_human_readable_u64 - pr_human_readable_s64 These obey printbuf->si_units - pr_units_u64 - pr_units_s64 These obey both printbuf-human_readable_units and printbuf->si_units Signed-off-by: Kent Overstreet --- include/linux/printbuf.h | 15 +++++++++++ lib/printbuf.c | 57 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/include/linux/printbuf.h b/include/linux/printbuf.h index 798e067457..c7919ae721 100644 --- a/include/linux/printbuf.h +++ b/include/linux/printbuf.h @@ -53,10 +53,19 @@ * * Make sure you use pr_newline() instead of \n in the format string for i= ndent * level and tabstops to work corretly. + * + * Output units: printbuf->units exists to tell pretty-printers how to out= put + * numbers: a raw value (e.g. directly from a superblock field), as bytes,= or as + * human readable bytes. pr_units() and pr_sectors() obey it. */ =20 #include =20 +enum printbuf_si { + PRINTBUF_UNITS_2, /* use binary powers of 2^10 */ + PRINTBUF_UNITS_10, /* use powers of 10^3 (standard SI) */ +}; + struct printbuf { char *buf; unsigned size; @@ -70,6 +79,8 @@ struct printbuf { u8 atomic; bool allocation_failure:1; bool heap_allocated:1; + enum printbuf_si si_units:1; + bool human_readable_units:1; u8 tabstop; u8 tabstops[4]; }; @@ -83,6 +94,10 @@ void pr_indent_add(struct printbuf *, unsigned); void pr_indent_sub(struct printbuf *, unsigned); void pr_tab(struct printbuf *); void pr_tab_rjust(struct printbuf *); +void pr_human_readable_u64(struct printbuf *, u64); +void pr_human_readable_s64(struct printbuf *, s64); +void pr_units_u64(struct printbuf *, u64); +void pr_units_s64(struct printbuf *, s64); =20 /* Initializer for a heap allocated printbuf: */ #define PRINTBUF ((struct printbuf) { .heap_allocated =3D true }) diff --git a/lib/printbuf.c b/lib/printbuf.c index c9f730a215..b99d4b0dff 100644 --- a/lib/printbuf.c +++ b/lib/printbuf.c @@ -9,6 +9,7 @@ #endif =20 #include +#include #include =20 static inline size_t printbuf_linelen(struct printbuf *buf) @@ -193,3 +194,59 @@ void pr_tab_rjust(struct printbuf *buf) buf->tabstop++; } EXPORT_SYMBOL(pr_tab_rjust); + +/** + * pr_human_readable_u64 - Print out a u64 in human readable units + * + * Units of 2^10 (default) or 10^3 are controlled via @buf->si_units + */ +void pr_human_readable_u64(struct printbuf *buf, u64 v) +{ + printbuf_make_room(buf, 10); + buf->pos +=3D string_get_size(v, 1, !buf->si_units, + buf->buf + buf->pos, + printbuf_remaining(buf)); +} +EXPORT_SYMBOL(pr_human_readable_u64); + +/** + * pr_human_readable_s64 - Print out a s64 in human readable units + * + * Units of 2^10 (default) or 10^3 are controlled via @buf->si_units + */ +void pr_human_readable_s64(struct printbuf *buf, s64 v) +{ + if (v < 0) + pr_char(buf, '-'); + pr_human_readable_u64(buf, abs(v)); +} +EXPORT_SYMBOL(pr_human_readable_s64); + +/** + * pr_human_readable_u64 - Print out a u64 according to printbuf unit opti= ons + * + * Units are either raw (default), or human reabable units (controlled via + * @buf->human_readable_units) + */ +void pr_units_u64(struct printbuf *out, u64 v) +{ + if (out->human_readable_units) + pr_human_readable_u64(out, v); + else + pr_buf(out, "%llu", v); +} +EXPORT_SYMBOL(pr_units_u64); + +/** + * pr_human_readable_s64 - Print out a s64 according to printbuf unit opti= ons + * + * Units are either raw (default), or human reabable units (controlled via + * @buf->human_readable_units) + */ +void pr_units_s64(struct printbuf *out, s64 v) +{ + if (v < 0) + pr_char(out, '-'); + pr_units_u64(out, v); +} +EXPORT_SYMBOL(pr_units_s64); --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A720C433EF for ; Thu, 19 May 2022 17:25:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242762AbiESRZm (ORCPT ); Thu, 19 May 2022 13:25:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242614AbiESRY5 (ORCPT ); Thu, 19 May 2022 13:24:57 -0400 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1862EA5A89; Thu, 19 May 2022 10:24:52 -0700 (PDT) Received: by mail-pg1-x535.google.com with SMTP id r71so5698894pgr.0; Thu, 19 May 2022 10:24:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Urg/PvF7N1ruHRzs7i4TJND15oOuTHDLyrPznJj/Hxc=; b=kTKRvi5qJaK55Z+CM6m2hFF158cWTIrDq1cpr8ksrG84+TEp9Z6KiVTWGUzHVB8YYV 0p9Ze1zUdqX7m8vsXQGGfHgyellma0Wg7BG7+dEGNN8ad0gJibx1We05zemp/dd1mfrn 2aiUH4k3DmJeFL1+mYSW+kHGW3WQJJAiOXreAXx+97i2D7JQplVGjC657IA0hHhP4LMM ABfE9IfONuyvAwZuGR0dx9Sk1mjozX9iiMxlyWScmAvst9l8iv8EOmpvktKav0qFWb/c oq+NSeuRiYVv1R/OrzqiD/z7kSfIuzIE+GnuLcJ2LK68FWtXB6JgYbB3OmXpHZQ2Q2Ki FjmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Urg/PvF7N1ruHRzs7i4TJND15oOuTHDLyrPznJj/Hxc=; b=qYQUSHePWh5NRh1y8/HCWbUvxjutUayEev/amLzH/uo/axSCJzn2m0smWtPW70+Oze GRw+Gpn5FTRKSpWrRwG1DpTpTZa34nFEJb5rITsW7MyZ0evLU0vo13Rv/TdDi24qPLPp ybdjAeEgQB/53kQFNIessiVHJnUTb491HvCJqx1kI8WRuWDDHRi5swD5Lq/d4fQqlQpy JzBwBG+Mg+LZW1aZGUgE2L+AaeXjXs96oajH9nqAJAYq9obNciqhqlfsVi+Ufbm1u3sK o8fyoiLFPhwg5oPJhhoGEiAES3476S1FNb9evZYA4Td66A6oCyh0sBIl1FQwB7lHunhv iqSQ== X-Gm-Message-State: AOAM531LNhpvoEfUwZa6hWn+mfHbqbcm9DWGp6YXaFYJ812C0WxnOTiN dZ9ogk4+Y7HxhZEjuD/nszhYu2eG8X46 X-Google-Smtp-Source: ABdhPJx4+FufxxmnsEncGa92MP0Kjx8yaN3r2hPBFqNyVm/e1rZxhPueIPCJCjmEAYSmUbuJiesaWw== X-Received: by 2002:a63:1312:0:b0:3f2:678b:d166 with SMTP id i18-20020a631312000000b003f2678bd166mr4877001pgl.279.1652981091163; Thu, 19 May 2022 10:24:51 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.24.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:24:50 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 08/28] lib/pretty-printers: pr_string_option(), pr_bitflags() Date: Thu, 19 May 2022 13:24:01 -0400 Message-Id: <20220519172421.162394-9-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Signed-off-by: Kent Overstreet --- include/linux/pretty-printers.h | 10 ++++++ lib/Makefile | 2 +- lib/pretty-printers.c | 58 +++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 include/linux/pretty-printers.h create mode 100644 lib/pretty-printers.c diff --git a/include/linux/pretty-printers.h b/include/linux/pretty-printer= s.h new file mode 100644 index 0000000000..2e8b6b4426 --- /dev/null +++ b/include/linux/pretty-printers.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ +/* Copyright (C) 2022 Kent Overstreet */ + +#ifndef _LINUX_PRETTY_PRINTERS_H +#define _LINUX_PRETTY_PRINTERS_H + +void pr_string_option(struct printbuf *, const char * const[], size_t); +void pr_bitflags(struct printbuf *, const char * const[], u64); + +#endif /* _LINUX_PRETTY_PRINTERS_H */ diff --git a/lib/Makefile b/lib/Makefile index 31a3904eda..98e9aefe62 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -34,7 +34,7 @@ lib-y :=3D ctype.o string.o vsprintf.o cmdline.o \ is_single_threaded.o plist.o decompress.o kobject_uevent.o \ earlycpio.o seq_buf.o siphash.o dec_and_lock.o \ nmi_backtrace.o nodemask.o win_minmax.o memcat_p.o \ - buildid.o printbuf.o + buildid.o printbuf.o pretty-printers.o =20 lib-$(CONFIG_PRINTK) +=3D dump_stack.o lib-$(CONFIG_SMP) +=3D cpumask.o diff --git a/lib/pretty-printers.c b/lib/pretty-printers.c new file mode 100644 index 0000000000..d794648ef9 --- /dev/null +++ b/lib/pretty-printers.c @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: LGPL-2.1+ +/* Copyright (C) 2022 Kent Overstreet */ + +#include +#include + +/** + * pr_string_option - Given a list of strings, print out the list and indi= cate + * which option is selected, with square brackets (sysfs style) + * + * @out: The printbuf to output to + * @list: List of strings to choose from + * @selected: The option to highlight, with square brackets + */ +void pr_string_option(struct printbuf *out, + const char * const list[], + size_t selected) +{ + size_t i; + + for (i =3D 0; list[i]; i++) { + if (i) + pr_char(out, ' '); + if (i =3D=3D selected) + pr_char(out, '['); + pr_str(out, list[i]); + if (i =3D=3D selected) + pr_char(out, ']'); + } +} +EXPORT_SYMBOL(pr_string_option); + +/** + * pr_bitflags: Given a bitmap and a list of names for each bit, print out= which + * bits are on, comma separated + * + * @out: The printbuf to output to + * @list: List of names for each bit + * @flags: Bits to print + */ +void pr_bitflags(struct printbuf *out, + const char * const list[], u64 flags) +{ + unsigned bit, nr =3D 0; + bool first =3D true; + + while (list[nr]) + nr++; + + while (flags && (bit =3D __ffs(flags)) < nr) { + if (!first) + pr_buf(out, ","); + first =3D false; + pr_str(out, list[bit]); + flags ^=3D 1 << bit; + } +} +EXPORT_SYMBOL(pr_bitflags); --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C756FC433EF for ; Thu, 19 May 2022 17:26:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234463AbiESRZt (ORCPT ); Thu, 19 May 2022 13:25:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242627AbiESRY5 (ORCPT ); Thu, 19 May 2022 13:24:57 -0400 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5059DA777E; Thu, 19 May 2022 10:24:56 -0700 (PDT) Received: by mail-pj1-x102b.google.com with SMTP id ds11so5942390pjb.0; Thu, 19 May 2022 10:24:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rmKCBHh5NxpYnol0Ho2uBrT6Afu7PiBh/Bpj7m21S4k=; b=gFaR6gU1uMZeJeeoNbMzrqeNhoMaLQsGmQCLKLTP4rE0vEeKawniiuRp+ZWLn1Jw6D LBx3R8yU0CiRZBHrxDrFSl2PHVUacXE9GuglsO/CpI//aIXMA5gSGySRIN4oVorTeMJg 5WKzTyDono1BJeHCgHKTCVyGUi8tvWgqOnleL+CFsKB5gvMIDs1Vfm9gdt2sC0Dmkn0K sDqI0muvLGzC25u1oAaS7fn92xWLIXW0ivI4ReeuAjmOPrpAVI6sRGzcmbugNru9VIua e5JP44R66OchdZnYepsoBjNQeCD/hw9yq4UvDYPoKG9dmvQ+jVas8h62i99LwgQS5I/O fJFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rmKCBHh5NxpYnol0Ho2uBrT6Afu7PiBh/Bpj7m21S4k=; b=r306ApMtGhrYYIPeN7L4oxOsMloUCvVUZVrLF3N+hpX1Upeh6KNdh9nxJTBovUmKxd 8uT2S9/SOcwscDVbXBDqz9nAFuRSRqSoYQa77Z3tKuC3vbp1mHegjSSMEWTPXF/7UfKH i4/txpXlNa0DH55Mm3mUREn9313fvfEFdgiwPFbzpw20myCRWc/VdpllbYic66KI+gBo OQpuBqdVVw23VqV7kO9i61HRnxYIxnV2rcOufKRaWohWnUsHA9P+NPOdUEyILIQ+fjcR QxLNEBO93Z/7RY0TYvFvsD0SAvOPL022hkhGBBWgrNTFw787y7TKybzT5RgE3UDeu1v+ wvGA== X-Gm-Message-State: AOAM530/OHINd+JUjrEC6k8UxfO346NvdZOhgu6W5A83q/kjfCwWs5uf NJSXNv2vEcC1ocbAFvc9WPCDpwXFScyA X-Google-Smtp-Source: ABdhPJxrKQV4yThEwK1aZau9KxCinawmnP2Nv/f6pXqjMKEeaQMJ/dYjTo12S2bWXcYke1p0ojuKgQ== X-Received: by 2002:a17:90b:1d10:b0:1dc:dea8:d2ad with SMTP id on16-20020a17090b1d1000b001dcdea8d2admr6756875pjb.174.1652981095252; Thu, 19 May 2022 10:24:55 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.24.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:24:54 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 09/28] vsprintf: Improve number() Date: Thu, 19 May 2022 13:24:02 -0400 Message-Id: <20220519172421.162394-10-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This patch refactors number() to make it a bit clearer, and it also changes it to call printbuf_make_room() only once at the start, instead of in the printbuf output helpers. Signed-off-by: Kent Overstreet --- lib/vsprintf.c | 84 ++++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 7fbeaf50d1..d1372c0b37 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -453,92 +453,94 @@ void number(struct printbuf *out, unsigned long long = num, { /* put_dec requires 2-byte alignment of the buffer. */ char tmp[3 * sizeof(num)] __aligned(2); - char sign; - char locase; + char sign =3D 0; + /* locase =3D 0 or 0x20. ORing digits or letters with 'locase' + * produces same digits or (maybe lowercased) letters */ + char locase =3D (spec.flags & SMALL); int need_pfx =3D ((spec.flags & SPECIAL) && spec.base !=3D 10); - int i; bool is_zero =3D num =3D=3D 0LL; int field_width =3D spec.field_width; int precision =3D spec.precision; + int nr_digits =3D 0; + int output_bytes =3D 0; =20 - /* locase =3D 0 or 0x20. ORing digits or letters with 'locase' - * produces same digits or (maybe lowercased) letters */ - locase =3D (spec.flags & SMALL); if (spec.flags & LEFT) spec.flags &=3D ~ZEROPAD; - sign =3D 0; if (spec.flags & SIGN) { if ((signed long long)num < 0) { sign =3D '-'; num =3D -(signed long long)num; - field_width--; + output_bytes++; } else if (spec.flags & PLUS) { sign =3D '+'; - field_width--; + output_bytes++; } else if (spec.flags & SPACE) { sign =3D ' '; - field_width--; + output_bytes++; } } if (need_pfx) { if (spec.base =3D=3D 16) - field_width -=3D 2; + output_bytes +=3D 2; else if (!is_zero) - field_width--; + output_bytes++; } =20 /* generate full string in tmp[], in reverse order */ - i =3D 0; - if (num < spec.base) - tmp[i++] =3D hex_asc_upper[num] | locase; - else if (spec.base !=3D 10) { /* 8 or 16 */ + if (spec.base =3D=3D 10) { + nr_digits =3D put_dec(tmp, num) - tmp; + } else { /* 8 or 16 */ int mask =3D spec.base - 1; - int shift =3D 3; + int shift =3D ilog2((unsigned) spec.base); =20 - if (spec.base =3D=3D 16) - shift =3D 4; do { - tmp[i++] =3D (hex_asc_upper[((unsigned char)num) & mask] | locase); + tmp[nr_digits++] =3D (hex_asc_upper[((unsigned char)num) & mask] | loca= se); num >>=3D shift; } while (num); - } else { /* base 10 */ - i =3D put_dec(tmp, num) - tmp; } =20 /* printing 100 using %2d gives "100", not "00" */ - if (i > precision) - precision =3D i; + precision =3D max(nr_digits, precision); + output_bytes +=3D precision; + field_width =3D max(0, field_width - output_bytes); + + printbuf_make_room(out, field_width + output_bytes); + /* leading space padding */ - field_width =3D max(0, field_width - precision); if (!(spec.flags & (ZEROPAD | LEFT))) { - pr_chars(out, ' ', field_width); + __pr_chars_reserved(out, ' ', field_width); field_width =3D 0; } + /* sign */ if (sign) - __pr_char(out, sign); + __pr_char_reserved(out, sign); + /* "0x" / "0" prefix */ if (need_pfx) { if (spec.base =3D=3D 16 || !is_zero) - __pr_char(out, '0'); + __pr_char_reserved(out, '0'); if (spec.base =3D=3D 16) - __pr_char(out, 'X' | locase); + __pr_char_reserved(out, 'X' | locase); } - /* zero or space padding */ - if (!(spec.flags & LEFT)) { - char c =3D ' ' + (spec.flags & ZEROPAD); =20 - pr_chars(out, c, field_width); - field_width =3D 0; - } - /* hmm even more zero padding? */ - if (precision > i) - pr_chars(out, '0', precision - i); + /* zero padding */ + if (!(spec.flags & LEFT)) + __pr_chars_reserved(out, '0', field_width); + + /* zero padding from precision */ + if (precision > nr_digits) + __pr_chars_reserved(out, '0', precision - nr_digits); + /* actual digits of result */ - while (--i >=3D 0) - __pr_char(out, tmp[i]); + while (--nr_digits >=3D 0) + __pr_char_reserved(out, tmp[nr_digits]); + /* trailing space padding */ - pr_chars(out, ' ', field_width); + if (spec.flags & LEFT) + __pr_chars_reserved(out, ' ', field_width); + + printbuf_nul_terminate(out); } =20 static noinline_for_stack --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A31F2C433F5 for ; Thu, 19 May 2022 17:26:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242836AbiESRZ7 (ORCPT ); Thu, 19 May 2022 13:25:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242607AbiESRY7 (ORCPT ); Thu, 19 May 2022 13:24:59 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7038CA30A5; Thu, 19 May 2022 10:24:58 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id bh5so5376893plb.6; Thu, 19 May 2022 10:24:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=e3OxyC3/DI33JXHwVUL35gvyfpdRWxFslUVcm3O0EXs=; b=gIY+kSSVUC4Mo1TlPJ2EpffwqI+g7vs9uSicukEY6JHyWUdCZrcpOcFeFlHxlp/9CT O1dorvDVkiR3qdq+ZTIE0axAWgk3elhD5DnL1fdP7mQY05KF77ubjlrR2z0JouMqOw3k 9S6Nkgstt4va5hpQ6rjkaxN6uefqKHXkHRyojR3uPmSoNz0OfO5j1bijGobBv1C7oKsR Bm7KgWf+cPb03mm6lQjxWHjabmxEyEwgdqmpCjPLIZgBP9NUbGqzjqdIcxVFiFlQC4xS QbKoK4pHfgoYpe/GsRl4Dwo5zwxyYmnxp1i63apv1e8r/U5Q4tblR+7VvBQij8KQhGIO xnVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=e3OxyC3/DI33JXHwVUL35gvyfpdRWxFslUVcm3O0EXs=; b=S5JbkDsMCc+7Wf1Py1DXxgvyCMCFZcRJDrwDhqiR6xB6jtZ62nK524yCLqjFgOb2+4 olUiVw8qAD7U/qLxDBo7oyuw8U2P5f0F0uAZoJGdL48s+v/imXheDAmUfdzME6UvsAut MuhqR+sgxFzRQ21AeFpup1vVGM5KyExGCmgd209n/JN/meO3usWYJaoL3K5ERMzvLa+y j5DD1muj9tMXOl6VUzBMsakaQylVKwptyC8PHlCLlgnPSKNIS4g1iFDsOAMwIzjZoX71 o8pfDqwZnj4NF44rmtNB82QD+wdKgYIktNUb2ZlejHOSYWUTGZgZmpNn/p7/rbz7bOlH E2jQ== X-Gm-Message-State: AOAM530QC8ZT8pAypUvVucPDFiNGVu9SLBpu2q5JEocbaB73OJpYmAuc i61vMIfmIaIb5ozsK2Z3yBkMpmiCrtCt X-Google-Smtp-Source: ABdhPJysdaEJkG8ndxjZfsUp39aQC6MnGASv6eEov7HDfqLiNgQj3nda4/tl6+uPmbOCpJn3pJB3Jg== X-Received: by 2002:a17:90b:1d87:b0:1dc:a9c0:3d49 with SMTP id pf7-20020a17090b1d8700b001dca9c03d49mr6875780pjb.12.1652981097648; Thu, 19 May 2022 10:24:57 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.24.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:24:56 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 10/28] vsprintf: pr_u64_minwidth(), pr_u64() Date: Thu, 19 May 2022 13:24:03 -0400 Message-Id: <20220519172421.162394-11-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This adds two new-style printbuf helpers for printing simple u64s, and converts num_to_str() to be a simple wrapper around pr_u64_minwidth(). Signed-off-by: Kent Overstreet --- lib/vsprintf.c | 100 ++++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 52 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index d1372c0b37..3f5638d27a 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -363,41 +363,51 @@ char *put_dec(char *buf, unsigned long long n) =20 #endif =20 -/* - * Convert passed number to decimal string. - * Returns the length of string. On buffer overflow, returns 0. - * - * If speed is not important, use snprintf(). It's easy to read the code. +/** + * pr_u64_minwidth - print a u64, in decimal, with zero padding + * @out: printbuf to output to + * @num: u64 to print + * @width: minimum width */ -int num_to_str(char *buf, int size, unsigned long long num, unsigned int w= idth) +void pr_u64_minwidth(struct printbuf *out, u64 num, unsigned width) { /* put_dec requires 2-byte alignment of the buffer. */ char tmp[sizeof(num) * 3] __aligned(2); - int idx, len; + unsigned len =3D put_dec(tmp, num) - tmp; =20 - /* put_dec() may work incorrectly for num =3D 0 (generate "", not "0") */ - if (num <=3D 9) { - tmp[0] =3D '0' + num; - len =3D 1; - } else { - len =3D put_dec(tmp, num) - tmp; - } + printbuf_make_room(out, max(len, width)); =20 - if (len > size || width > size) - return 0; + if (width > len) + __pr_chars_reserved(out, '0', width - len); =20 - if (width > len) { - width =3D width - len; - for (idx =3D 0; idx < width; idx++) - buf[idx] =3D ' '; - } else { - width =3D 0; - } + while (len) + __pr_char_reserved(out, tmp[--len]); + printbuf_nul_terminate(out); +} =20 - for (idx =3D 0; idx < len; ++idx) - buf[idx + width] =3D tmp[len - idx - 1]; +/** + * pr_u64 - print a simple u64, in decimal + * @out: printbuf to output to + * @num: u64 to print + */ +void pr_u64(struct printbuf *out, u64 num) +{ + pr_u64_minwidth(out, num, 0); +} =20 - return len + width; +/* + * Convert passed number to decimal string. + * Returns the length of string. On buffer overflow, returns 0. + * + * Consider switching to printbufs and using pr_u64() or pr_u64_minwith() + * instead. + */ +int num_to_str(char *buf, int size, unsigned long long num, unsigned int w= idth) +{ + struct printbuf out =3D PRINTBUF_EXTERN(buf, size); + + pr_u64_minwidth(&out, num, width); + return out.pos; } =20 #define SIGN 1 /* unsigned/signed, must be 1 */ @@ -996,20 +1006,6 @@ static const struct printf_spec default_dec_spec =3D { .precision =3D -1, }; =20 -static const struct printf_spec default_dec02_spec =3D { - .base =3D 10, - .field_width =3D 2, - .precision =3D -1, - .flags =3D ZEROPAD, -}; - -static const struct printf_spec default_dec04_spec =3D { - .base =3D 10, - .field_width =3D 4, - .precision =3D -1, - .flags =3D ZEROPAD, -}; - static noinline_for_stack void resource_string(struct printbuf *out, struct resource *res, struct printf_spec spec, const char *fmt) @@ -1208,12 +1204,12 @@ void bitmap_list_string(struct printbuf *out, unsig= ned long *bitmap, __pr_char(out, ','); first =3D false; =20 - number(out, rbot, default_dec_spec); + pr_u64(out, rbot); if (rtop =3D=3D rbot + 1) continue; =20 - __pr_char(out, '-'); - number(out, rtop - 1, default_dec_spec); + pr_char(out, '-'); + pr_u64(out, rtop - 1); } } =20 @@ -1762,21 +1758,21 @@ void date_str(struct printbuf *out, int year =3D tm->tm_year + (r ? 0 : 1900); int mon =3D tm->tm_mon + (r ? 0 : 1); =20 - number(out, year, default_dec04_spec); - __pr_char(out, '-'); - number(out, mon, default_dec02_spec); - __pr_char(out, '-'); - number(out, tm->tm_mday, default_dec02_spec); + pr_u64_minwidth(out, year, 4); + pr_char(out, '-'); + pr_u64_minwidth(out, mon, 2); + pr_char(out, '-'); + pr_u64_minwidth(out, tm->tm_mday, 2); } =20 static noinline_for_stack void time_str(struct printbuf *out, const struct rtc_time *tm, bool r) { - number(out, tm->tm_hour, default_dec02_spec); + pr_u64_minwidth(out, tm->tm_hour, 2); __pr_char(out, ':'); - number(out, tm->tm_min, default_dec02_spec); + pr_u64_minwidth(out, tm->tm_min, 2); __pr_char(out, ':'); - number(out, tm->tm_sec, default_dec02_spec); + pr_u64_minwidth(out, tm->tm_sec, 2); } =20 static noinline_for_stack @@ -2054,7 +2050,7 @@ void device_node_string(struct printbuf *out, struct = device_node *dn, str_spec.precision =3D precision; break; case 'p': /* phandle */ - number(out, (unsigned int)dn->phandle, default_dec_spec); + pr_u64(out, (unsigned int)dn->phandle); break; case 'P': /* path-spec */ p =3D fwnode_get_name(of_fwnode_handle(dn)); --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8488CC433EF for ; Thu, 19 May 2022 17:26:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242625AbiESR0T (ORCPT ); Thu, 19 May 2022 13:26:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242640AbiESRZD (ORCPT ); Thu, 19 May 2022 13:25:03 -0400 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88EACAEE07; Thu, 19 May 2022 10:25:01 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id qe3-20020a17090b4f8300b001dc24e4da73so6418786pjb.1; Thu, 19 May 2022 10:25:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XYDmKGQ68/tcSrl96epbwKIv/5DWUEdmV+ljYHPziEQ=; b=Wp2efo+C3K4Ivu4rvZOFIJAHwQ1ryFWKaVFECGdFWYf87IEmUdHKWbf39P0c8fjElA m2yhaUhZSkZYA8QBoNRgLuBFeyNoxNx7hAqOpTF8naCZgfSkltJUNtVQPwImHyLGcOJi N79mOU+o9K0PoHhwtD9Fe6ei10ET+UKlwvluDjUohaEIP+Yfgsl9gPiw/j1LWL71Mck3 tFIw0Ao3/ApVsYMepzbAJdcH5E4lvLlPivsBpMbNGrIRY48dwZFfdeXce0Q5comOVhM7 CCpvTcsqpsv/gA2iaEkNGWYJbFQjqfyuFJ8qoBUP82VrPYvkvNccFnLpO1T1P2xPHQMl iTvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XYDmKGQ68/tcSrl96epbwKIv/5DWUEdmV+ljYHPziEQ=; b=wZB/YBbxc69xsy4f/NivuDS7aiIJ8Y0tDjpmN80KKZ+7uIYa9QLmc5NlN0BfhyV22D +LtCwQauJx2qgDkWlChk6IqKQoxTl3GO8lyEFDLvZABCQvw4tbb1JDnxi+DQFLdTeRWR 7bMXoDuEam7x1k3tSsxubAiKK4wg2jqiNUTfgYWOyhVUbuOPRMGuqAHn7GsHoo7/9AqR l8RoaxVTO63+DQQEvmC/bMOzpSyn3YzllkuR+kD5RFlzVLVNPnfzi8L2m2at1owYZ8Fi aswPKXCe22pZbMPVwlhAO60uRaYSHYtRt4ZWTk09/LTUxexCNRkCubxThJg93TASlb1A rJSA== X-Gm-Message-State: AOAM533NsdrB6Sl//EVU2+Kq5WmNUmSfOJAO+Z09DlOzQj3JG2NNM5By b/jj3l5m3puaPyoiGIoNf9eY9sZpoNgS X-Google-Smtp-Source: ABdhPJwV/SeCuBwi09/4GacP5ySX3VYfZpArZWbWdS9Qs6GE+kBNtfBoB95oICb/LCleM8twL0QxRQ== X-Received: by 2002:a17:90a:f81:b0:1df:91b5:f0e3 with SMTP id 1-20020a17090a0f8100b001df91b5f0e3mr6809170pjz.227.1652981100183; Thu, 19 May 2022 10:25:00 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.24.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:24:59 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 11/28] vsprintf: Lift pr_hex_bytes() out from hex_string() Date: Thu, 19 May 2022 13:24:04 -0400 Message-Id: <20220519172421.162394-12-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This factors pr_hex_bytes(), a new printbuf-style helper, out from hex_string and adds it to pretty-printers.c. Signed-off-by: Kent Overstreet --- include/linux/pretty-printers.h | 1 + lib/pretty-printers.c | 23 +++++++++++++++++++++++ lib/vsprintf.c | 13 ++++--------- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/include/linux/pretty-printers.h b/include/linux/pretty-printer= s.h index 2e8b6b4426..ded34622e8 100644 --- a/include/linux/pretty-printers.h +++ b/include/linux/pretty-printers.h @@ -4,6 +4,7 @@ #ifndef _LINUX_PRETTY_PRINTERS_H #define _LINUX_PRETTY_PRINTERS_H =20 +void pr_hex_bytes(struct printbuf *, const u8 *, unsigned, unsigned); void pr_string_option(struct printbuf *, const char * const[], size_t); void pr_bitflags(struct printbuf *, const char * const[], u64); =20 diff --git a/lib/pretty-printers.c b/lib/pretty-printers.c index d794648ef9..162e6865f9 100644 --- a/lib/pretty-printers.c +++ b/lib/pretty-printers.c @@ -4,6 +4,29 @@ #include #include =20 +/** + * pr_hex_bytes - Print a string of hex bytes, with optional separator + * + * @out: The printbuf to output to + * @addr: Buffer to print + * @nr: Number of bytes to print + * @separator: Optional separator character between each byte + */ +void pr_hex_bytes(struct printbuf *out, const u8 *addr, + unsigned nr, unsigned separator) +{ + unsigned i; + + for (i =3D 0; i < nr; ++i) { + if (separator && i) + pr_char(out, separator); + pr_hex_byte(out, addr[i]); + } + + printbuf_nul_terminate(out); +} +EXPORT_SYMBOL(pr_hex_bytes); + /** * pr_string_option - Given a list of strings, print out the list and indi= cate * which option is selected, with square brackets (sysfs style) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 3f5638d27a..d4293b4a40 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -52,6 +52,7 @@ #include /* cpu_to_le16 */ =20 #include +#include #include "kstrtox.h" =20 static noinline unsigned long long simple_strntoull(const char *startp, si= ze_t max_chars, char **endp, unsigned int base) @@ -1107,10 +1108,10 @@ void resource_string(struct printbuf *out, struct r= esource *res, } =20 static noinline_for_stack -void hex_string(struct printbuf *out, u8 *addr, +void hex_string(struct printbuf *out, const u8 *addr, struct printf_spec spec, const char *fmt) { - int i, len =3D 1; /* if we pass '%ph[CDN]', field width remains + int len =3D 1; /* if we pass '%ph[CDN]', field width remains negative value, fallback to the default */ char separator; =20 @@ -1139,13 +1140,7 @@ void hex_string(struct printbuf *out, u8 *addr, if (spec.field_width > 0) len =3D min_t(int, spec.field_width, 64); =20 - for (i =3D 0; i < len; ++i) { - __pr_char(out, hex_asc_hi(addr[i])); - __pr_char(out, hex_asc_lo(addr[i])); - - if (separator && i !=3D len - 1) - __pr_char(out, separator); - } + pr_hex_bytes(out, addr, len, separator); } =20 static noinline_for_stack --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B3408C4332F for ; Thu, 19 May 2022 17:26:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242856AbiESR0F (ORCPT ); Thu, 19 May 2022 13:26:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242623AbiESRZF (ORCPT ); Thu, 19 May 2022 13:25:05 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27049A3093; Thu, 19 May 2022 10:25:03 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id i24so5706065pfa.7; Thu, 19 May 2022 10:25:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sYyJmhJ1UJuNTKSNkznuVgdOa0YLmomkOmOhYpxJCFI=; b=B1Heee3hxpN0aBQABPk0xvOT+jNBu4aa7AW9HK6xt/c87pzEb/DTZi7S+zuAx7PfOi Ugk+cjgRwVIq0AfPCPScAmiJDJinvCqHIbGwupmjTb3JM3sMnmnbqB79XrgM5gwDVatp DltHbvP8sTARpXH5IhZ3fAthfr4j1lR5OO2INzRNnl9acSGvlZhHf2euPwAZVBTSJ8si CZbQIn3QSEfbI5qbXSHNv2G4GXSdwjhtMOznL6S5rKQFb+CMk73ITxWA1Gez162dP7UI oi9ial8S6TM8MW23ND5h0h8wcA+lsIW4YDX+5lQMe8sJ+MsFwpTlpFAx+9j2yenqSqi4 xHPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sYyJmhJ1UJuNTKSNkznuVgdOa0YLmomkOmOhYpxJCFI=; b=HHBc+sXsT0fHkYnrEEY3HGC35DqmQ0Mk3iwDPmrZGaQLvAbkYpgC1WolbKR0U86ZZ5 HFhrFTJBJKPx4b1SLgXEdrtq6j9oNLbaW4y7Io4aiBJQuSFyu6wI4Q6GC5Au3OsagDeQ t4KficODlqEQV8HFox/VQJEfryuh0N/V332UxYx/jIjIxa+eUBH90I9RfMv6xQv/O1nF 5wNRq3b6gPaqJsXHgXjp/ChgyzNIvhflpgHOLqbuXOKQmhgkW44Zj6vvk4i+8M7vsYnI wiC3cDME+A4gY2q6opet644fAy1FeYv97GkfYPb5VGGtiz7Qerq9nDe/NN4BlHtsKVzG HJsQ== X-Gm-Message-State: AOAM531aeALEZCBhiEVhH2z+HPphW74l9IlGs565S3AbMK1vBjo8isti SQZCbeG4FRcHVCtiNou3uov+zqngLk54 X-Google-Smtp-Source: ABdhPJzZY5YP8RElcJziKMe2Pb+9bzOWxPxFDsokqdhQLlpUQtMJ7p36gRVAP/eKYVsQomShn6d3Ew== X-Received: by 2002:a63:2c15:0:b0:3f6:6a5f:8f0a with SMTP id s21-20020a632c15000000b003f66a5f8f0amr495864pgs.76.1652981102152; Thu, 19 May 2022 10:25:02 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.25.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:25:01 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 12/28] test_printf: Drop requirement that sprintf not write past nul Date: Thu, 19 May 2022 13:24:05 -0400 Message-Id: <20220519172421.162394-13-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The current test code checks that sprintf never writes past the terminating nul. This is a rather strange requirement, completely separate from writing past the end of the buffer, which of course we can't do: writing anywhere to the buffer passed to snprintf, within size of course, should be perfectly fine. Since this check has no documentation as to where it comes from or what depends on it, and it's getting in the way of further refactoring (printf_spec handling is right now scattered massively throughout the code, and we'd like to consolidate it) - delete it. Also, many current pretty-printers building up their output on the stack, and then copy it to the actual output buffer - by eliminating this requirement we can kill those extra buffers. Signed-off-by: Kent Overstreet --- lib/test_printf.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/test_printf.c b/lib/test_printf.c index 1e1604ef1a..0804ab788d 100644 --- a/lib/test_printf.c +++ b/lib/test_printf.c @@ -78,12 +78,6 @@ do_test(int bufsize, const char *expect, int elen, return 1; } =20 - if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE = - (written + 1))) { - pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator= \n", - bufsize, fmt); - return 1; - } - if (memcmp(test_buffer, expect, written)) { pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n", bufsize, fmt, test_buffer, written, expect); --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 15859C433F5 for ; Thu, 19 May 2022 17:26:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240047AbiESR02 (ORCPT ); Thu, 19 May 2022 13:26:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242644AbiESRZI (ORCPT ); Thu, 19 May 2022 13:25:08 -0400 Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 017C6A5015; Thu, 19 May 2022 10:25:06 -0700 (PDT) Received: by mail-pf1-x432.google.com with SMTP id a11so5732211pff.1; Thu, 19 May 2022 10:25:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0Z2kNVHcs0MhZJmIj7x/+zDze1ru4sjeHsll7v+JHTQ=; b=ZkPr9ahoZHZ0TgU3UsWyaUbc23S0SH5oWnuYJi5dTO7X7FDHuXdErSUrkNhcy9VbzC 4wCX5CUAUmq9TWjU5dHsQUIVutA2sMBEhFl2cfy0aYRnHKSDXwq/t9C8NUtsxateoUKr n3uxu6y/G1snZsbmI7uX0FZeC0XIZqP/+sEHoflabUu1aIHpxbVFkVvhVeyOD7OFLVhN BQNrgJfzjYAD24UFiRdmcrFNml23b+WeLhf8m5pRgU1pMsB+a9415IWA3+tBt5tS9+Uj kl9+TGrZ4AF8JdN0I4XzKtLHdEO59aljRmY5/hwqzLnQjo62Bvqm0jecGKhDUjzfcvae EJtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0Z2kNVHcs0MhZJmIj7x/+zDze1ru4sjeHsll7v+JHTQ=; b=3Ffe3u6EE8lsNgn6eovHIjUiuOwMwvbn2a0XX9uyFYnZhpffg78C+qdgIso3f90dU5 hMGy9Z6YCae7Uxuz/i2PEvXVR6THf51xnrY1+BeVIRjpsTOn3H1w3NyeWFycqc+zW5Gt iauwdTukck9YcmVQb+wSvy6wzqqSU6yvL4VFFAJsxAcFEaA7/sKLabXluT7h8CEmaHNX j0v6grN5R3iZb9Q8rGopt2ekS+FEgPR99XNcuMqTvuD0HFhiDNZCSaTTe37Blw/kxGs/ LVBBSpocXDZNJQjc9NW4rZzG0r+ftDcN6E8p7sntj8aH+rdfLkR6JzRYGnR7kd8oBNYs bCWA== X-Gm-Message-State: AOAM533JS9h/s5EI41b3KWtjIiS2RDo+ibXpXq76JE5kAQDSwCDOL3bp Wf10e1JoaIjMrdTc5VETBgolt03srMIu X-Google-Smtp-Source: ABdhPJyQ4d8Ldgogj0mgkSSyH2jXGBhzIWwVXHbeP6HFGaB0YvxRNh7zC5J6SErqmrXxCag3dqAONQ== X-Received: by 2002:a63:5d1d:0:b0:3c6:480:1ab7 with SMTP id r29-20020a635d1d000000b003c604801ab7mr4775746pgb.228.1652981104471; Thu, 19 May 2022 10:25:04 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.25.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:25:03 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 13/28] vsprintf: Start consolidating printf_spec handling Date: Thu, 19 May 2022 13:24:06 -0400 Message-Id: <20220519172421.162394-14-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" printf_spec is right now something of a mess - it's a grab-bag of state that's interpreted inconsistently by different code, and it's scattered throughout vsprintf.c. We'd like to get it out of the pretty-printers, and have it be solely the responsibility of vsprintf()/vpr_buf(), the code that parses and handles format strings. Most of the code that uses printf_spec is only using it for a minimum & maximum field width - that can be done at the toplevel by checking how much we just printed, and padding or truncating it as necessary. This patch takes those "simple" uses of printf_spec and moves them as far up the call stack as possible. This patch also renames some helpers and creates new ones that don't take printf_spec: - do_width_precision: new helper that handles with/precision of printf_spec - error_string -> error_string_spec - check_pointer -> check_pointer_spec - string -> string_spec Next patches will be reducing/eliminating uses of the *_spec versions. Signed-off-by: Kent Overstreet --- lib/vsprintf.c | 248 +++++++++++++++++++++++++++---------------------- 1 file changed, 138 insertions(+), 110 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index d4293b4a40..6bbbb52bb0 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -613,6 +613,19 @@ void widen_string(struct printbuf *out, int n, pr_chars(out, ' ', spaces); } =20 +static void do_width_precision(struct printbuf *out, unsigned prev_pos, + struct printf_spec spec) +{ + unsigned n =3D out->pos - prev_pos; + + if (n > spec.precision) { + out->pos -=3D n - spec.precision; + n =3D spec.precision; + } + + widen_string(out, n, spec); +} + /* Handle string from a well known address. */ static void string_nocheck(struct printbuf *out, const char *s, @@ -645,7 +658,7 @@ static void err_ptr(struct printbuf *out, void *ptr, } =20 /* Be careful: error messages must fit into the given buffer. */ -static void error_string(struct printbuf *out, const char *s, +static void error_string_spec(struct printbuf *out, const char *s, struct printf_spec spec) { /* @@ -675,7 +688,7 @@ static const char *check_pointer_msg(const void *ptr) return NULL; } =20 -static int check_pointer(struct printbuf *out, +static int check_pointer_spec(struct printbuf *out, const void *ptr, struct printf_spec spec) { @@ -683,7 +696,7 @@ static int check_pointer(struct printbuf *out, =20 err_msg =3D check_pointer_msg(ptr); if (err_msg) { - error_string(out, err_msg, spec); + error_string_spec(out, err_msg, spec); return -EFAULT; } =20 @@ -691,16 +704,47 @@ static int check_pointer(struct printbuf *out, } =20 static noinline_for_stack -void string(struct printbuf *out, +void string_spec(struct printbuf *out, const char *s, struct printf_spec spec) { - if (check_pointer(out, s, spec)) + if (check_pointer_spec(out, s, spec)) return; =20 string_nocheck(out, s, spec); } =20 +static void error_string(struct printbuf *out, const char *s) +{ + /* + * Hard limit to avoid a completely insane messages. It actually + * works pretty well because most error messages are in + * the many pointer format modifiers. + */ + pr_bytes(out, s, min(strlen(s), 2 * sizeof(void *))); +} + +static int check_pointer(struct printbuf *out, const void *ptr) +{ + const char *err_msg; + + err_msg =3D check_pointer_msg(ptr); + if (err_msg) { + error_string(out, err_msg); + return -EFAULT; + } + + return 0; +} + +static void string(struct printbuf *out, const char *s) +{ + if (check_pointer(out, s)) + return; + + pr_str(out, s); +} + static void pointer_string(struct printbuf *out, const void *ptr, struct printf_spec spec) @@ -824,7 +868,7 @@ static void ptr_to_id(struct printbuf *out, if (ret) { spec.field_width =3D 2 * sizeof(ptr); /* string length must be less than default_width */ - return error_string(out, str, spec); + return error_string_spec(out, str, spec); } =20 pointer_string(out, (const void *)hashval, spec); @@ -851,7 +895,7 @@ void restricted_pointer(struct printbuf *out, if (in_irq() || in_serving_softirq() || in_nmi()) { if (spec.field_width =3D=3D -1) spec.field_width =3D 2 * sizeof(ptr); - return error_string(out, "pK-error", spec); + return error_string_spec(out, "pK-error", spec); } =20 /* @@ -881,14 +925,12 @@ void restricted_pointer(struct printbuf *out, } =20 static noinline_for_stack -void dentry_name(struct printbuf *out, - const struct dentry *d, struct printf_spec spec, +void dentry_name(struct printbuf *out, const struct dentry *d, const char *fmt) { - const char *array[4], *s; + const char *array[4]; const struct dentry *p; - int depth; - int i, n; + int i, depth; =20 switch (fmt[1]) { case '2': case '3': case '4': @@ -900,7 +942,7 @@ void dentry_name(struct printbuf *out, =20 rcu_read_lock(); for (i =3D 0; i < depth; i++, d =3D p) { - if (check_pointer(out, d, spec)) { + if (check_pointer(out, d)) { rcu_read_unlock(); return; } @@ -914,55 +956,46 @@ void dentry_name(struct printbuf *out, break; } } - s =3D array[--i]; - for (n =3D 0; n !=3D spec.precision; n++) { - char c =3D *s++; - if (!c) { - if (!i) - break; - c =3D '/'; - s =3D array[--i]; - } - __pr_char(out, c); + while (1) { + pr_str(out, array[--i]); + if (!i) + break; + __pr_char(out, '/'); } rcu_read_unlock(); - widen_string(out, n, spec); } =20 static noinline_for_stack -void file_dentry_name(struct printbuf *out, - const struct file *f, - struct printf_spec spec, const char *fmt) +void file_dentry_name(struct printbuf *out, const struct file *f, + const char *fmt) { - if (check_pointer(out, f, spec)) + if (check_pointer(out, f)) return; =20 - return dentry_name(out, f->f_path.dentry, spec, fmt); + return dentry_name(out, f->f_path.dentry, fmt); } #ifdef CONFIG_BLOCK static noinline_for_stack -void bdev_name(struct printbuf *out, - struct block_device *bdev, - struct printf_spec spec, const char *fmt) +void bdev_name(struct printbuf *out, struct block_device *bdev) { struct gendisk *hd; =20 - if (check_pointer(out, bdev, spec)) + if (check_pointer(out, bdev)) return; =20 hd =3D bdev->bd_disk; - string(out, hd->disk_name, spec); + string(out, hd->disk_name); if (bdev->bd_partno) { if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) __pr_char(out, 'p'); - number(out, bdev->bd_partno, spec); + pr_u64(out, bdev->bd_partno); } } #endif =20 static noinline_for_stack void symbol_string(struct printbuf *out, void *ptr, - struct printf_spec spec, const char *fmt) + const char *fmt) { unsigned long value; #ifdef CONFIG_KALLSYMS @@ -985,17 +1018,12 @@ void symbol_string(struct printbuf *out, void *ptr, else sprint_symbol_no_offset(sym, value); =20 - string_nocheck(out, sym, spec); + pr_str(out, sym); #else special_hex_number(out, value, sizeof(void *)); #endif } =20 -static const struct printf_spec default_str_spec =3D { - .field_width =3D -1, - .precision =3D -1, -}; - static const struct printf_spec default_flag_spec =3D { .base =3D 16, .precision =3D -1, @@ -1054,7 +1082,7 @@ void resource_string(struct printbuf *out, struct res= ource *res, int decode =3D (fmt[0] =3D=3D 'R') ? 1 : 0; const struct printf_spec *specp; =20 - if (check_pointer(out, res, spec)) + if (check_pointer_spec(out, res, spec)) return; =20 __pr_char(&sym, '['); @@ -1119,7 +1147,7 @@ void hex_string(struct printbuf *out, const u8 *addr, /* nothing to print */ return; =20 - if (check_pointer(out, addr, spec)) + if (check_pointer_spec(out, addr, spec)) return; =20 switch (fmt[1]) { @@ -1152,7 +1180,7 @@ void bitmap_string(struct printbuf *out, unsigned lon= g *bitmap, int i, chunksz; bool first =3D true; =20 - if (check_pointer(out, bitmap, spec)) + if (check_pointer_spec(out, bitmap, spec)) return; =20 /* reused to print numbers */ @@ -1191,7 +1219,7 @@ void bitmap_list_string(struct printbuf *out, unsigne= d long *bitmap, bool first =3D true; int rbot, rtop; =20 - if (check_pointer(out, bitmap, spec)) + if (check_pointer_spec(out, bitmap, spec)) return ; =20 for_each_set_bitrange(rbot, rtop, bitmap, nr_bits) { @@ -1218,7 +1246,7 @@ void mac_address_string(struct printbuf *out, u8 *add= r, char separator; bool reversed =3D false; =20 - if (check_pointer(out, addr, spec)) + if (check_pointer_spec(out, addr, spec)) return; =20 switch (fmt[1]) { @@ -1295,8 +1323,6 @@ void ip4_string(struct printbuf *out, __pr_char(out, '.'); index +=3D step; } - - printbuf_nul_terminate(out); } =20 static noinline_for_stack @@ -1525,7 +1551,7 @@ void ip_addr_string(struct printbuf *out, const void = *ptr, { char *err_fmt_msg; =20 - if (check_pointer(out, ptr, spec)) + if (check_pointer_spec(out, ptr, spec)) return; =20 switch (fmt[1]) { @@ -1546,12 +1572,12 @@ void ip_addr_string(struct printbuf *out, const voi= d *ptr, case AF_INET6: return ip6_addr_string_sa(out, &sa->v6, spec, fmt); default: - return error_string(out, "(einval)", spec); + return error_string_spec(out, "(einval)", spec); }} } =20 err_fmt_msg =3D fmt[0] =3D=3D 'i' ? "(%pi?)" : "(%pI?)"; - return error_string(out, err_fmt_msg, spec); + return error_string_spec(out, err_fmt_msg, spec); } =20 static noinline_for_stack @@ -1566,7 +1592,7 @@ void escaped_string(struct printbuf *out, u8 *addr, if (spec.field_width =3D=3D 0) return; /* nothing to print */ =20 - if (check_pointer(out, addr, spec)) + if (check_pointer_spec(out, addr, spec)) return; =20 do { @@ -1611,7 +1637,7 @@ static void va_format(struct printbuf *out, { va_list va; =20 - if (check_pointer(out, va_fmt, spec)) + if (check_pointer_spec(out, va_fmt, spec)) return; =20 va_copy(va, *va_fmt->va); @@ -1620,16 +1646,13 @@ static void va_format(struct printbuf *out, } =20 static noinline_for_stack -void uuid_string(struct printbuf *out, const u8 *addr, - struct printf_spec spec, const char *fmt) +void uuid_string(struct printbuf *out, const u8 *addr, const char *fmt) { - char uuid_buf[UUID_STRING_LEN + 1]; - struct printbuf uuid =3D PRINTBUF_EXTERN(uuid_buf, sizeof(uuid_buf)); int i; const u8 *index =3D uuid_index; bool uc =3D false; =20 - if (check_pointer(out, addr, spec)) + if (check_pointer(out, addr)) return; =20 switch (*(++fmt)) { @@ -1646,32 +1669,28 @@ void uuid_string(struct printbuf *out, const u8 *ad= dr, =20 for (i =3D 0; i < 16; i++) { if (uc) - pr_hex_byte_upper(&uuid, addr[index[i]]); + pr_hex_byte_upper(out, addr[index[i]]); else - pr_hex_byte(&uuid, addr[index[i]]); + pr_hex_byte(out, addr[index[i]]); switch (i) { case 3: case 5: case 7: case 9: - __pr_char(&uuid, '-'); + __pr_char(out, '-'); break; } } - - printbuf_nul_terminate(&uuid); - - string_nocheck(out, uuid_buf, spec); } =20 static noinline_for_stack void netdev_bits(struct printbuf *out, const void *addr, - struct printf_spec spec, const char *fmt) + const char *fmt) { unsigned long long num; int size; =20 - if (check_pointer(out, addr, spec)) + if (check_pointer(out, addr)) return; =20 switch (fmt[1]) { @@ -1681,7 +1700,7 @@ void netdev_bits(struct printbuf *out, const void *ad= dr, special_hex_number(out, num, size); break; default: - error_string(out, "(%pN?)", spec); + error_string(out, "(%pN?)"); break; } } @@ -1696,9 +1715,9 @@ void fourcc_string(struct printbuf *out, const u32 *f= ourcc, u32 val; =20 if (fmt[1] !=3D 'c' || fmt[2] !=3D 'c') - return error_string(out, "(%p4?)", spec); + return error_string_spec(out, "(%p4?)", spec); =20 - if (check_pointer(out, fourcc, spec)) + if (check_pointer_spec(out, fourcc, spec)) return; =20 val =3D *fourcc & ~BIT(31); @@ -1718,17 +1737,17 @@ void fourcc_string(struct printbuf *out, const u32 = *fourcc, __pr_char(&output, ')'); printbuf_nul_terminate(&output); =20 - string(out, output_buf, spec); + string_spec(out, output_buf, spec); } =20 static noinline_for_stack void address_val(struct printbuf *out, const void *addr, - struct printf_spec spec, const char *fmt) + const char *fmt) { unsigned long long num; int size; =20 - if (check_pointer(out, addr, spec)) + if (check_pointer(out, addr)) return; =20 switch (fmt[1]) { @@ -1779,7 +1798,7 @@ void rtc_str(struct printbuf *out, const struct rtc_t= ime *tm, bool found =3D true; int count =3D 2; =20 - if (check_pointer(out, tm, spec)) + if (check_pointer_spec(out, tm, spec)) return; =20 switch (fmt[count]) { @@ -1849,7 +1868,7 @@ void time_and_date(struct printbuf *out, case 'T': return time64_str(out, *(const time64_t *)ptr, spec, fmt); default: - return error_string(out, "(%pt?)", spec); + return error_string_spec(out, "(%pt?)", spec); } } =20 @@ -1858,16 +1877,16 @@ void clock(struct printbuf *out, struct clk *clk, struct printf_spec spec, const char *fmt) { if (!IS_ENABLED(CONFIG_HAVE_CLK)) - return error_string(out, "(%pC?)", spec); + return error_string_spec(out, "(%pC?)", spec); =20 - if (check_pointer(out, clk, spec)) + if (check_pointer_spec(out, clk, spec)) return; =20 switch (fmt[1]) { case 'n': default: #ifdef CONFIG_COMMON_CLK - return string(out, __clk_get_name(clk), spec); + return string_spec(out, __clk_get_name(clk), spec); #else return ptr_to_id(out, clk, spec); #endif @@ -1885,7 +1904,7 @@ void format_flags(struct printbuf *out, unsigned long= flags, if ((flags & mask) !=3D mask) continue; =20 - string(out, names->name, default_str_spec); + string(out, names->name); =20 flags &=3D ~mask; if (flags) @@ -1943,7 +1962,7 @@ void format_page_flags(struct printbuf *out, unsigned= long flags) if (append) __pr_char(out, '|'); =20 - string(out, pff[i].name, default_str_spec); + string(out, pff[i].name); __pr_char(out, '=3D'); number(out, (flags >> pff[i].shift) & pff[i].mask, *pff[i].spec); =20 @@ -1959,7 +1978,7 @@ void flags_string(struct printbuf *out, void *flags_p= tr, unsigned long flags; const struct trace_print_flags *names; =20 - if (check_pointer(out, flags_ptr, spec)) + if (check_pointer_spec(out, flags_ptr, spec)) return; =20 switch (fmt[1]) { @@ -1974,7 +1993,7 @@ void flags_string(struct printbuf *out, void *flags_p= tr, names =3D gfpflag_names; break; default: - return error_string(out, "(%pG?)", spec); + return error_string_spec(out, "(%pG?)", spec); } =20 return format_flags(out, flags, names); @@ -1991,10 +2010,8 @@ void fwnode_full_name_string(struct printbuf *out, struct fwnode_handle *__fwnode =3D fwnode_get_nth_parent(fwnode, depth); =20 - string(out, fwnode_get_name_prefix(__fwnode), - default_str_spec); - string(out, fwnode_get_name(__fwnode), - default_str_spec); + string(out, fwnode_get_name_prefix(__fwnode)); + string(out, fwnode_get_name(__fwnode)); =20 fwnode_handle_put(__fwnode); } @@ -2015,12 +2032,12 @@ void device_node_string(struct printbuf *out, struc= t device_node *dn, str_spec.field_width =3D -1; =20 if (fmt[0] !=3D 'F') - return error_string(out, "(%pO?)", spec); + return error_string_spec(out, "(%pO?)", spec); =20 if (!IS_ENABLED(CONFIG_OF)) - return error_string(out, "(%pOF?)", spec); + return error_string_spec(out, "(%pOF?)", spec); =20 - if (check_pointer(out, dn, spec)) + if (check_pointer_spec(out, dn, spec)) return; =20 /* simple case without anything any more format specifiers */ @@ -2041,7 +2058,7 @@ void device_node_string(struct printbuf *out, struct = device_node *dn, p =3D fwnode_get_name(of_fwnode_handle(dn)); precision =3D str_spec.precision; str_spec.precision =3D strchrnul(p, '@') - p; - string(out, p, str_spec); + string_spec(out, p, str_spec); str_spec.precision =3D precision; break; case 'p': /* phandle */ @@ -2051,7 +2068,7 @@ void device_node_string(struct printbuf *out, struct = device_node *dn, p =3D fwnode_get_name(of_fwnode_handle(dn)); if (!p[1]) p =3D "/"; - string(out, p, str_spec); + string_spec(out, p, str_spec); break; case 'F': /* flags */ tbuf[0] =3D of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-'; @@ -2061,18 +2078,18 @@ void device_node_string(struct printbuf *out, struc= t device_node *dn, tbuf[4] =3D 0; string_nocheck(out, tbuf, str_spec); break; - case 'c': /* major compatible string */ + case 'c': /* major compatible string_spec */ ret =3D of_property_read_string(dn, "compatible", &p); if (!ret) - string(out, p, str_spec); + string_spec(out, p, str_spec); break; - case 'C': /* full compatible string */ + case 'C': /* full compatible string_spec */ has_mult =3D false; of_property_for_each_string(dn, "compatible", prop, p) { if (has_mult) string_nocheck(out, ",", str_spec); string_nocheck(out, "\"", str_spec); - string(out, p, str_spec); + string_spec(out, p, str_spec); string_nocheck(out, "\"", str_spec); =20 has_mult =3D true; @@ -2097,16 +2114,16 @@ void fwnode_string(struct printbuf *out, str_spec.field_width =3D -1; =20 if (*fmt !=3D 'w') - return error_string(out, "(%pf?)", spec); + return error_string_spec(out, "(%pf?)", spec); =20 - if (check_pointer(out, fwnode, spec)) + if (check_pointer_spec(out, fwnode, spec)) return; =20 fmt++; =20 switch (*fmt) { case 'P': /* name */ - string(out, fwnode_get_name(fwnode), str_spec); + string_spec(out, fwnode_get_name(fwnode), str_spec); break; case 'f': /* full_name */ default: @@ -2277,13 +2294,16 @@ static noinline_for_stack void pointer(struct printbuf *out, const char *fmt, void *ptr, struct printf_spec spec) { + unsigned prev_pos =3D out->pos; + switch (*fmt) { case 'S': case 's': ptr =3D dereference_symbol_descriptor(ptr); fallthrough; case 'B': - return symbol_string(out, ptr, spec, fmt); + symbol_string(out, ptr, fmt); + return do_width_precision(out, prev_pos, spec); case 'R': case 'r': return resource_string(out, ptr, spec, fmt); @@ -2314,28 +2334,34 @@ void pointer(struct printbuf *out, const char *fmt, case 'E': return escaped_string(out, ptr, spec, fmt); case 'U': - return uuid_string(out, ptr, spec, fmt); + uuid_string(out, ptr, fmt); + return do_width_precision(out, prev_pos, spec); case 'V': return va_format(out, ptr, spec, fmt); case 'K': return restricted_pointer(out, ptr, spec); case 'N': - return netdev_bits(out, ptr, spec, fmt); + netdev_bits(out, ptr, fmt); + return do_width_precision(out, prev_pos, spec); case '4': return fourcc_string(out, ptr, spec, fmt); case 'a': - return address_val(out, ptr, spec, fmt); + address_val(out, ptr, fmt); + return do_width_precision(out, prev_pos, spec); case 'd': - return dentry_name(out, ptr, spec, fmt); + dentry_name(out, ptr, fmt); + return do_width_precision(out, prev_pos, spec); case 't': return time_and_date(out, ptr, spec, fmt); case 'C': return clock(out, ptr, spec, fmt); case 'D': - return file_dentry_name(out, ptr, spec, fmt); + file_dentry_name(out, ptr, fmt); + return do_width_precision(out, prev_pos, spec); #ifdef CONFIG_BLOCK case 'g': - return bdev_name(out, ptr, spec, fmt); + bdev_name(out, ptr); + return do_width_precision(out, prev_pos, spec); #endif =20 case 'G': @@ -2355,9 +2381,9 @@ void pointer(struct printbuf *out, const char *fmt, case 'k': switch (fmt[1]) { case 's': - return string(out, ptr, spec); + return string_spec(out, ptr, spec); default: - return error_string(out, "(einval)", spec); + return error_string_spec(out, "(einval)", spec); } } =20 @@ -2665,6 +2691,7 @@ void vpr_buf(struct printbuf *out, const char *fmt, v= a_list args) =20 while (*fmt) { const char *old_fmt =3D fmt; + unsigned prev_pos =3D out->pos; int read =3D format_decode(fmt, &spec); =20 fmt +=3D read; @@ -2694,7 +2721,8 @@ void vpr_buf(struct printbuf *out, const char *fmt, v= a_list args) break; =20 case FORMAT_TYPE_STR: - string(out, va_arg(args, char *), spec); + string(out, va_arg(args, char *)); + do_width_precision(out, prev_pos, spec); break; =20 case FORMAT_TYPE_PTR: @@ -3206,7 +3234,7 @@ int bstr_printf(char *buf, size_t size, const char *f= mt, const u32 *bin_buf) case FORMAT_TYPE_STR: { const char *str_arg =3D args; args +=3D strlen(str_arg) + 1; - string(&out, (char *)str_arg, spec); + string_spec(&out, (char *)str_arg, spec); break; } =20 --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7A593C433F5 for ; Thu, 19 May 2022 17:26:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242611AbiESR0d (ORCPT ); Thu, 19 May 2022 13:26:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242649AbiESRZM (ORCPT ); Thu, 19 May 2022 13:25:12 -0400 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F2B6A7761; Thu, 19 May 2022 10:25:08 -0700 (PDT) Received: by mail-pf1-x436.google.com with SMTP id bo5so5721580pfb.4; Thu, 19 May 2022 10:25:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=DWycBowt0MidQ3WMMJDlkVMAwAS6HurrbLWqpcpIeew=; b=ieIBkesiUG3WbVarQoJ3CVBrLVFF2ZgD6sCot+rKZuv+OcNpsC6ETR0Lkb6mMVWyVL pHLD93Z/XR1VmVtIRlTMCg1ijeR+uhfcUWxaklIlWX103icxzWuStv2Ru6+Hyw9XcJWr z4lvta76RkjqqL2GH/buE+jfWpq4r6vydBeEBiRHdH5qAQMYLv6YC2eql7OigwD/M7Ha B4Ozc9QQCbc/UZ7WItYHxX+sItsW83QLixIgJudfBGAfRrm/n1QRgEP3/PrtNqsXCLoY 4GDaSKBec+mMr3bT3abk8YT+01q+0bTKb3wZvNck7rInlkdLEdTpArcz+fLfcnCL/BgO 1f+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=DWycBowt0MidQ3WMMJDlkVMAwAS6HurrbLWqpcpIeew=; b=ODlNt0+AuU2Iqrqzs3cGflmjJ0MwOVKaKDDyAeVelvih9SXtciqA7bgW86fZoj7/uU 8xWep8S1dM/gBmjuOWumP1zwMVjSQ7yo0QncDnLFzdHYDu+47cAe1v7osnRaruqfbxlP pUcnDxbknkScl8GZRtHE/stUweJOYRUtifFLNhPP2+l45mCPY+yVgUnKrZtVMfRr9PTE QUSTuozX5c2BlJzuycn45ORM+sHzZagcF1CJ+tQHo4wcUXmQ9zjHtWaW4KXN9Pg9mGjr KposcQUqY8oAD0nb2oaKbhl5vda+6iUkyujXGMsbcaeGQRGaOd4ozVYJy7hWTJyYhfXY 75VA== X-Gm-Message-State: AOAM533r+01UhC6g3mhZrwDzhWiwpdAZS6bTZYQoTSaee3ScRkd1MXB5 ATzZpjvrdNI2d0n1QC7ZVctfaDigh8iq X-Google-Smtp-Source: ABdhPJwTg7ITskD+TKuxgCGFKwkICgKnJY2ce2LXZmQUnJk+rxl5keXm7fNU8h11Mv7ipoMF9BHOYg== X-Received: by 2002:a62:6dc3:0:b0:505:895a:d38b with SMTP id i186-20020a626dc3000000b00505895ad38bmr5888441pfc.7.1652981107652; Thu, 19 May 2022 10:25:07 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.25.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:25:06 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 14/28] vsprintf: Refactor resource_string() Date: Thu, 19 May 2022 13:24:07 -0400 Message-Id: <20220519172421.162394-15-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Two changes: - We're attempting to consolidate printf_spec and format string handling in the top level vpr_buf(), this changes resource_string to not take printf_spec - With the new printbuf helpers there's no need to use a separate stack allocated buffer, so this patch deletes it. Signed-off-by: Kent Overstreet --- lib/vsprintf.c | 52 +++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 6bbbb52bb0..dd4bbb28e7 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -1037,7 +1037,7 @@ static const struct printf_spec default_dec_spec =3D { =20 static noinline_for_stack void resource_string(struct printbuf *out, struct resource *res, - struct printf_spec spec, const char *fmt) + int decode) { #ifndef IO_RSRC_PRINTK_SIZE #define IO_RSRC_PRINTK_SIZE 6 @@ -1076,63 +1076,58 @@ void resource_string(struct printbuf *out, struct r= esource *res, #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") - char sym_buf[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, - 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; - struct printbuf sym =3D PRINTBUF_EXTERN(sym_buf, sizeof(sym_buf)); - int decode =3D (fmt[0] =3D=3D 'R') ? 1 : 0; const struct printf_spec *specp; =20 - if (check_pointer_spec(out, res, spec)) + if (check_pointer(out, res)) return; =20 - __pr_char(&sym, '['); + __pr_char(out, '['); if (res->flags & IORESOURCE_IO) { - string_nocheck(&sym, "io ", str_spec); + string_nocheck(out, "io ", str_spec); specp =3D &io_spec; } else if (res->flags & IORESOURCE_MEM) { - string_nocheck(&sym, "mem ", str_spec); + string_nocheck(out, "mem ", str_spec); specp =3D &mem_spec; } else if (res->flags & IORESOURCE_IRQ) { - string_nocheck(&sym, "irq ", str_spec); + string_nocheck(out, "irq ", str_spec); specp =3D &default_dec_spec; } else if (res->flags & IORESOURCE_DMA) { - string_nocheck(&sym, "dma ", str_spec); + string_nocheck(out, "dma ", str_spec); specp =3D &default_dec_spec; } else if (res->flags & IORESOURCE_BUS) { - string_nocheck(&sym, "bus ", str_spec); + string_nocheck(out, "bus ", str_spec); specp =3D &bus_spec; } else { - string_nocheck(&sym, "??? ", str_spec); + string_nocheck(out, "??? ", str_spec); specp =3D &mem_spec; decode =3D 0; } if (decode && res->flags & IORESOURCE_UNSET) { - string_nocheck(&sym, "size ", str_spec); - number(&sym, resource_size(res), *specp); + string_nocheck(out, "size ", str_spec); + number(out, resource_size(res), *specp); } else { - number(&sym, res->start, *specp); + number(out, res->start, *specp); if (res->start !=3D res->end) { - __pr_char(&sym, '-'); - number(&sym, res->end, *specp); + __pr_char(out, '-'); + number(out, res->end, *specp); } } if (decode) { if (res->flags & IORESOURCE_MEM_64) - string_nocheck(&sym, " 64bit", str_spec); + string_nocheck(out, " 64bit", str_spec); if (res->flags & IORESOURCE_PREFETCH) - string_nocheck(&sym, " pref", str_spec); + string_nocheck(out, " pref", str_spec); if (res->flags & IORESOURCE_WINDOW) - string_nocheck(&sym, " window", str_spec); + string_nocheck(out, " window", str_spec); if (res->flags & IORESOURCE_DISABLED) - string_nocheck(&sym, " disabled", str_spec); + string_nocheck(out, " disabled", str_spec); } else { - string_nocheck(&sym, " flags ", str_spec); - number(&sym, res->flags, default_flag_spec); + string_nocheck(out, " flags ", str_spec); + number(out, res->flags, default_flag_spec); } - __pr_char(&sym, ']'); - printbuf_nul_terminate(&sym); + __pr_char(out, ']'); =20 - string_nocheck(out, sym_buf, spec); + printbuf_nul_terminate(out); } =20 static noinline_for_stack @@ -2306,7 +2301,8 @@ void pointer(struct printbuf *out, const char *fmt, return do_width_precision(out, prev_pos, spec); case 'R': case 'r': - return resource_string(out, ptr, spec, fmt); + resource_string(out, ptr, fmt[0] =3D=3D 'R'); + return do_width_precision(out, prev_pos, spec); case 'h': return hex_string(out, ptr, spec, fmt); case 'b': --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C964EC433EF for ; Thu, 19 May 2022 17:26:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242644AbiESR0k (ORCPT ); Thu, 19 May 2022 13:26:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242663AbiESRZO (ORCPT ); Thu, 19 May 2022 13:25:14 -0400 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0E83AF1C3; Thu, 19 May 2022 10:25:10 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id j6so5682712pfe.13; Thu, 19 May 2022 10:25:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=T/71y4H0I4tJDzEESJX0yVgmBrZLbwRkc6IKYDc6cJQ=; b=FNeKL719/JSvBNGGdlxYJrA++Pqq2AgXDBKMjEHguJLVlJF/71rwvQzK/58eD+ilYz 1hSU78Lyg69ebMTAqvNqtbbxK7VCSIEGgkfiHGqQnky43Nz08btAj78Jegp3YSTYfoME NTi7ADeDSjO3y05gXnLjuF1oQyymuRHEFnpJTlrRWZiIxFT0ZjvrHAD+DNqliS+9hMwV KiztEkiGOMbmQaMQfdbx0Ewk31VGeVpvOWXiK0xgHqlNTvz97i7Z6OHBB9+UxVDTducT ncQ7PyrFuYWLsGsHpq0SlJaSgIzYWuQkctoju9ro+b2clTsKwQhLELeFU8G2iQzEfV4f +Qwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=T/71y4H0I4tJDzEESJX0yVgmBrZLbwRkc6IKYDc6cJQ=; b=h2XMIk4g8BZd2czELH8/cRoBmMjF1CnPRdcZxhYkb8YvquzavpUXaKnx8CMC4G7PqF lgP4+g+z3DJk+j3ECYsz3D41nKI73XrDah3LWlu2qLB+A3yPK48/ssAWAlz07emv+Pg+ LETAqUX/M+LAujw4aYDJFqyQfvrBP5b1qx29RNmcHqYN+vNFI5NVbamCYjQgfRm++6ug KhkJeliOwGkUNxfWoFFSnYhYStgeLNVp1iCas4vCMPAGdH9PXl3vvPuPpVLbhAAJcnpc Kj+A/i3drbg17lW2zZC9mBnSErS5Ty0cHw9b6kTgaMR/in4QMlMBpC+bwehf9AhLNCMM J5cA== X-Gm-Message-State: AOAM53012or8PR8R/SaPz6Intne/wOgVIIGUeAsnGK7T9WQBGlSbPuzU AINtmVu2Oygp8/sR+P1L0dS0FTWLTc0x X-Google-Smtp-Source: ABdhPJynGDjof146Uv8mxfj7qsya6dFrrvJU47qw8CwICqf8Ilv6hZa/vLYj0yeXSEPe63TM26xXeg== X-Received: by 2002:a65:6205:0:b0:3f5:d436:5446 with SMTP id d5-20020a656205000000b003f5d4365446mr4703783pgv.532.1652981109886; Thu, 19 May 2022 10:25:09 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.25.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:25:09 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 15/28] vsprintf: Refactor fourcc_string() Date: Thu, 19 May 2022 13:24:08 -0400 Message-Id: <20220519172421.162394-16-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" - We're attempting to consolidate printf_spec and format string handling in the top level vpr_buf(), this changes fourcc_string() to not take printf_spec - With the new printbuf helpers there's no need to use a separate stack allocated buffer, so this patch deletes it. Signed-off-by: Kent Overstreet --- lib/vsprintf.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index dd4bbb28e7..3d17ddad31 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -1702,17 +1702,15 @@ void netdev_bits(struct printbuf *out, const void *= addr, =20 static noinline_for_stack void fourcc_string(struct printbuf *out, const u32 *fourcc, - struct printf_spec spec, const char *fmt) + const char *fmt) { - char output_buf[sizeof("0123 little-endian (0x01234567)")]; - struct printbuf output =3D PRINTBUF_EXTERN(output_buf, sizeof(output_buf)= ); unsigned int i; u32 val; =20 if (fmt[1] !=3D 'c' || fmt[2] !=3D 'c') - return error_string_spec(out, "(%p4?)", spec); + return error_string(out, "(%p4?)"); =20 - if (check_pointer_spec(out, fourcc, spec)) + if (check_pointer(out, fourcc)) return; =20 val =3D *fourcc & ~BIT(31); @@ -1721,18 +1719,17 @@ void fourcc_string(struct printbuf *out, const u32 = *fourcc, unsigned char c =3D val >> (i * 8); =20 /* Print non-control ASCII characters as-is, dot otherwise */ - __pr_char(&output, isascii(c) && isprint(c) ? c : '.'); + __pr_char(out, isascii(c) && isprint(c) ? c : '.'); } =20 - pr_str(&output, *fourcc & BIT(31) ? " big-endian" : " little-endian"); + pr_str(out, *fourcc & BIT(31) ? " big-endian" : " little-endian"); =20 - __pr_char(&output, ' '); - __pr_char(&output, '('); - special_hex_number(&output, *fourcc, sizeof(u32)); - __pr_char(&output, ')'); - printbuf_nul_terminate(&output); + __pr_char(out, ' '); + __pr_char(out, '('); + special_hex_number(out, *fourcc, sizeof(u32)); + __pr_char(out, ')'); =20 - string_spec(out, output_buf, spec); + printbuf_nul_terminate(out); } =20 static noinline_for_stack @@ -2340,7 +2337,8 @@ void pointer(struct printbuf *out, const char *fmt, netdev_bits(out, ptr, fmt); return do_width_precision(out, prev_pos, spec); case '4': - return fourcc_string(out, ptr, spec, fmt); + fourcc_string(out, ptr, fmt); + return do_width_precision(out, prev_pos, spec); case 'a': address_val(out, ptr, fmt); return do_width_precision(out, prev_pos, spec); --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 84589C433F5 for ; Thu, 19 May 2022 17:26:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242720AbiESR0t (ORCPT ); Thu, 19 May 2022 13:26:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242664AbiESRZO (ORCPT ); Thu, 19 May 2022 13:25:14 -0400 Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0431A5019; Thu, 19 May 2022 10:25:12 -0700 (PDT) Received: by mail-pf1-x42b.google.com with SMTP id c14so5729202pfn.2; Thu, 19 May 2022 10:25:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=teMqOk605wE7rUEuFa8rRtNMPBYif8vlfnxLj41/R8s=; b=RhLKemdeyKMoaO8/D7bl3XzrMy3eOARd40G6avAxat24s5KPh9KNOXNZBsTaKFNNZA URVsxFq7jsqNmeO1Zz75Bw9lTKStQQxXgFDrarQpK1jFC6FvHVHlT8Ph8aCGSZtRovAS mwFLG85EBXYt4+gvjt7J3Ncm9/bi4YVwDp0b9A8rT8wBpL2KbdQAMgEPfeOYFaJkcL2N hdeumnwhHY1E9Dtj7Pkr9kn0GwpRRm6bpBpp8LD3cO16RTl3lCouCs1PEEfkWcZoB47M tVCS9Ws9/pcvzvR9A90sQg4Hl9H+0NsRfi5fopz/8ePZUK1Y9myJJeIqAesjHKoKPkma OQzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=teMqOk605wE7rUEuFa8rRtNMPBYif8vlfnxLj41/R8s=; b=WZKneUQmlddI/8YGEMJzHMkI0A9WOYg5hhA9K9j2TQj40B+haIGRGaRk9NpwaF73Rx oiaP5k1ruo4B1upoM4G+ku+Hs6pzb4KifSFCnNey2OKtYVbHX2DN1Lr1BfJHz95GzTrM yx+iXAKQ7Sv6Bqf8VuTCe4ZNZw4jC1H+7cUGFD6oTWTpASWDzJ+fMDS5PDcDja2rQaSm WPu9rAztjWdXPXJEeUHrExE30nD+iPpRVc7h5dD7P+poR07xE+oXIr+9wNU/xT0f2gr6 kHNNljmoJGAu5JIINSRsyNReyifujjPF9Eo2e+CwWIFk6/M4sQt5P9ua8DpRTy4OD8TJ 8Jrg== X-Gm-Message-State: AOAM530Picoxc0g6lGUUMemVcFdYLLbUfRX11CQmlFCphriEXrsH5mWr PEKmUQy5K0eubyiM1BlVHUZ8CXmo8l5T X-Google-Smtp-Source: ABdhPJxmG4yWmQYM6MYp7ey8C6E2XXkNpCtkpekZPD9kwoH4kpFBMfn/+DU75HGQBkBCiYnuoFeWRA== X-Received: by 2002:a63:e017:0:b0:3f2:543b:8402 with SMTP id e23-20020a63e017000000b003f2543b8402mr4763071pgh.209.1652981111766; Thu, 19 May 2022 10:25:11 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.25.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:25:11 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 16/28] vsprintf: Refactor ip_addr_string() Date: Thu, 19 May 2022 13:24:09 -0400 Message-Id: <20220519172421.162394-17-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" - We're attempting to consolidate printf_spec and format string handling in the top level vpr_buf(), this changes ip_addr_string() to not take printf_spec - With the new printbuf helpers there's no need to use a separate stack allocated buffer, so this patch deletes it. Signed-off-by: Kent Overstreet --- lib/vsprintf.c | 116 ++++++++++++++++--------------------------------- 1 file changed, 37 insertions(+), 79 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 3d17ddad31..62a221522c 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -1273,13 +1273,13 @@ void mac_address_string(struct printbuf *out, u8 *a= ddr, } =20 static noinline_for_stack -void ip4_string(struct printbuf *out, - const u8 *addr, const char *fmt) +void ip4_string(struct printbuf *out, const u8 *addr, const char *fmt) { - int i; - bool leading_zeros =3D (fmt[0] =3D=3D 'i'); - int index; - int step; + struct printf_spec spec =3D default_dec_spec; + int i, index, step; + + if (fmt[0] =3D=3D 'i') + spec.precision =3D 3; =20 switch (fmt[2]) { case 'h': @@ -1303,19 +1303,9 @@ void ip4_string(struct printbuf *out, break; } for (i =3D 0; i < 4; i++) { - char temp[4] __aligned(2); /* hold each IP quad in reverse order */ - int digits =3D put_dec_trunc8(temp, addr[index]) - temp; - if (leading_zeros) { - if (digits < 3) - __pr_char(out, '0'); - if (digits < 2) - __pr_char(out, '0'); - } - /* reverse the digits in the quad */ - while (digits--) - __pr_char(out, temp[digits]); - if (i < 3) + if (i) __pr_char(out, '.'); + number(out, addr[index], spec); index +=3D step; } } @@ -1398,8 +1388,6 @@ void ip6_compressed_string(struct printbuf *out, cons= t char *addr) __pr_char(out, ':'); ip4_string(out, &in6.s6_addr[12], "I4"); } - - printbuf_nul_terminate(out); } =20 static noinline_for_stack @@ -1419,41 +1407,20 @@ void ip6_string(struct printbuf *out, const char *a= ddr, const char *fmt) =20 static noinline_for_stack void ip6_addr_string(struct printbuf *out, const u8 *addr, - struct printf_spec spec, const char *fmt) + const char *fmt) { - char ip6_addr_buf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")= ]; - struct printbuf ip6_addr =3D PRINTBUF_EXTERN(ip6_addr_buf, sizeof(ip6_add= r_buf)); - if (fmt[0] =3D=3D 'I' && fmt[2] =3D=3D 'c') - ip6_compressed_string(&ip6_addr, addr); + ip6_compressed_string(out, addr); else - ip6_string(&ip6_addr, addr, fmt); - - string_nocheck(out, ip6_addr_buf, spec); -} - -static noinline_for_stack -void ip4_addr_string(struct printbuf *out, const u8 *addr, - struct printf_spec spec, const char *fmt) -{ - char ip4_addr_buf[sizeof("255.255.255.255")]; - struct printbuf ip4_addr =3D PRINTBUF_EXTERN(ip4_addr_buf, sizeof(ip4_add= r_buf)); - - ip4_string(&ip4_addr, addr, fmt); - - string_nocheck(out, ip4_addr_buf, spec); + ip6_string(out, addr, fmt); } =20 static noinline_for_stack void ip6_addr_string_sa(struct printbuf *out, const struct sockaddr_in6 *sa, - struct printf_spec spec, const char *fmt) + const char *fmt) { bool have_p =3D false, have_s =3D false, have_f =3D false, have_c =3D fal= se; - char ip6_addr_buf[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]= ") + - sizeof(":12345") + sizeof("/123456789") + - sizeof("%1234567890")]; - struct printbuf ip6_addr =3D PRINTBUF_EXTERN(ip6_addr_buf, sizeof(ip6_add= r_buf)); const u8 *addr =3D (const u8 *) &sa->sin6_addr; char fmt6[2] =3D { fmt[0], '6' }; =20 @@ -1476,42 +1443,35 @@ void ip6_addr_string_sa(struct printbuf *out, } =20 if (have_p || have_s || have_f) - __pr_char(&ip6_addr, '['); + __pr_char(out, '['); =20 if (fmt6[0] =3D=3D 'I' && have_c) - ip6_compressed_string(&ip6_addr, addr); + ip6_compressed_string(out, addr); else - ip6_string(&ip6_addr, addr, fmt6); + ip6_string(out, addr, fmt6); =20 if (have_p || have_s || have_f) - __pr_char(&ip6_addr, ']'); + __pr_char(out, ']'); =20 if (have_p) { - __pr_char(&ip6_addr, ':'); - number(&ip6_addr, ntohs(sa->sin6_port), spec); + __pr_char(out, ':'); + pr_u64(out, ntohs(sa->sin6_port)); } if (have_f) { - __pr_char(&ip6_addr, '/'); - number(&ip6_addr, ntohl(sa->sin6_flowinfo & - IPV6_FLOWINFO_MASK), spec); + __pr_char(out, '/'); + pr_u64(out, ntohl(sa->sin6_flowinfo & IPV6_FLOWINFO_MASK)); } if (have_s) { - __pr_char(&ip6_addr, '%'); - number(&ip6_addr, sa->sin6_scope_id, spec); + __pr_char(out, '%'); + pr_u64(out, sa->sin6_scope_id); } - printbuf_nul_terminate(&ip6_addr); - - string_nocheck(out, ip6_addr_buf, spec); } =20 static noinline_for_stack -void ip4_addr_string_sa(struct printbuf *out, - const struct sockaddr_in *sa, - struct printf_spec spec, const char *fmt) +void ip4_addr_string_sa(struct printbuf *out, const struct sockaddr_in *sa, + const char *fmt) { bool have_p =3D false; - char ip4_addr_buf[sizeof("255.255.255.255") + sizeof(":12345")]; - struct printbuf ip4_addr =3D PRINTBUF_EXTERN(ip4_addr_buf, sizeof(ip4_add= r_buf)); const u8 *addr =3D (const u8 *) &sa->sin_addr.s_addr; char fmt4[3] =3D { fmt[0], '4', 0 }; =20 @@ -1530,30 +1490,27 @@ void ip4_addr_string_sa(struct printbuf *out, } } =20 - ip4_string(&ip4_addr, addr, fmt4); + ip4_string(out, addr, fmt4); if (have_p) { - __pr_char(&ip4_addr, ':'); - number(&ip4_addr, ntohs(sa->sin_port), spec); + __pr_char(out, ':'); + pr_u64(out, ntohs(sa->sin_port)); } - printbuf_nul_terminate(&ip4_addr); - - string_nocheck(out, ip4_addr_buf, spec); } =20 static noinline_for_stack void ip_addr_string(struct printbuf *out, const void *ptr, - struct printf_spec spec, const char *fmt) + const char *fmt) { char *err_fmt_msg; =20 - if (check_pointer_spec(out, ptr, spec)) + if (check_pointer(out, ptr)) return; =20 switch (fmt[1]) { case '6': - return ip6_addr_string(out, ptr, spec, fmt); + return ip6_addr_string(out, ptr, fmt); case '4': - return ip4_addr_string(out, ptr, spec, fmt); + return ip4_string(out, ptr, fmt); case 'S': { const union { struct sockaddr raw; @@ -1563,16 +1520,16 @@ void ip_addr_string(struct printbuf *out, const voi= d *ptr, =20 switch (sa->raw.sa_family) { case AF_INET: - return ip4_addr_string_sa(out, &sa->v4, spec, fmt); + return ip4_addr_string_sa(out, &sa->v4, fmt); case AF_INET6: - return ip6_addr_string_sa(out, &sa->v6, spec, fmt); + return ip6_addr_string_sa(out, &sa->v6, fmt); default: - return error_string_spec(out, "(einval)", spec); + return error_string(out, "(einval)"); }} } =20 err_fmt_msg =3D fmt[0] =3D=3D 'i' ? "(%pi?)" : "(%pI?)"; - return error_string_spec(out, err_fmt_msg, spec); + error_string(out, err_fmt_msg); } =20 static noinline_for_stack @@ -2323,7 +2280,8 @@ void pointer(struct printbuf *out, const char *fmt, * 4: 001.002.003.004 * 6: 000102...0f */ - return ip_addr_string(out, ptr, spec, fmt); + ip_addr_string(out, ptr, fmt); + return do_width_precision(out, prev_pos, spec); case 'E': return escaped_string(out, ptr, spec, fmt); case 'U': --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4F112C433EF for ; Thu, 19 May 2022 17:27:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234200AbiESR1G (ORCPT ); Thu, 19 May 2022 13:27:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242634AbiESRZR (ORCPT ); Thu, 19 May 2022 13:25:17 -0400 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD595A5013; Thu, 19 May 2022 10:25:14 -0700 (PDT) Received: by mail-pj1-x1036.google.com with SMTP id n10so5900134pjh.5; Thu, 19 May 2022 10:25:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=io/JXzBTUQk4XZSNyvTx+ccbiuHm3ThRzq8pqtgnkls=; b=n7JngmyoE82ipNzOKgXY2Db70Xg2ETtdjgIKTaLPnJnUZj7RqY/32C7MLh65YJW9vB iz/naU/m40oekxL1UG80eUUDObsReWUYEFWvNMSHQ31i0paJKyLGSyz+XiPJFBPYPP+o PAhw7CNPHhNzdPRJzLrxKAKuRJXN04zbdh0K+BzSg/CdxwHGRQwx+U/29eTVayAGeNfT ugZDNRanE4OevAqP2+pzE4KsQ1xJ5TUG1cUDhGG8FDKkQU18hkId3W7kw5IPLdJzwlDE h9AcGbbHjXqrdeyBmHeeiWYxfML7UjV0kv8D9PXo+sKJJBAjzvmcE3zcbpbJOBmgulFg E9HA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=io/JXzBTUQk4XZSNyvTx+ccbiuHm3ThRzq8pqtgnkls=; b=qcQcSGPi1vk+lHPL5UrXKdN9ZAmpiEEb6ShyRQiapjWorhuWj+26Ig6vHrxuBO8LT+ suXfjmkufnRajk9zM2x5FAa+K4eBhC6wwwqp9G9XhEjZbaQoAMDjbE4AnEJDL/A9EGCD JrJPR5lzU2+000ANsgFVd+2+d56D4L0aP2Q/gc2LwGG761JiZfGP0fFhmm8zX/958l6y 3SyZAErIsc2ljpMHLUCY+F1N2twsbo3ciY2jrw6poFSW0MDnsgqQEaSiVUb+xPOiRjcP 9tDAvPp7Esvj9iQ9u9X7YUMvBStiBRjKxbndCpxl16Vfmi7gpPSGp/LQF1XUIBgwSqN7 LMOA== X-Gm-Message-State: AOAM532pAdRUycocNmWe++qXePssXTEBV+Dh25mZQPZx5FnnzXNZnFst LBRbtSYW6Zfg1gCKNSXS5Nr8JfjEOtzk X-Google-Smtp-Source: ABdhPJycqfU8mbGxYoOraR7qmIBu84SScDm9vM7ADw0HK604nHc/1qmJ1gzvNgAv2oi8BonybDwaMw== X-Received: by 2002:a17:90b:3506:b0:1df:d8b8:6eb3 with SMTP id ls6-20020a17090b350600b001dfd8b86eb3mr4246254pjb.104.1652981113811; Thu, 19 May 2022 10:25:13 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.25.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:25:13 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 17/28] vsprintf: Refactor mac_address_string() Date: Thu, 19 May 2022 13:24:10 -0400 Message-Id: <20220519172421.162394-18-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" - We're attempting to consolidate printf_spec and format string handling in the top level vpr_buf(), this changes mac_address_string() to not take printf_spec - With the new printbuf helpers there's no need to use a separate stack allocated buffer, so this patch deletes it. Signed-off-by: Kent Overstreet --- lib/vsprintf.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 62a221522c..76947e0d30 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -1233,15 +1233,13 @@ void bitmap_list_string(struct printbuf *out, unsig= ned long *bitmap, =20 static noinline_for_stack void mac_address_string(struct printbuf *out, u8 *addr, - struct printf_spec spec, const char *fmt) + const char *fmt) { - char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; - char *p =3D mac_addr; int i; char separator; bool reversed =3D false; =20 - if (check_pointer_spec(out, addr, spec)) + if (check_pointer(out, addr)) return; =20 switch (fmt[1]) { @@ -1260,16 +1258,13 @@ void mac_address_string(struct printbuf *out, u8 *a= ddr, =20 for (i =3D 0; i < 6; i++) { if (reversed) - p =3D hex_byte_pack(p, addr[5 - i]); + pr_hex_byte(out, addr[5 - i]); else - p =3D hex_byte_pack(p, addr[i]); + pr_hex_byte(out, addr[i]); =20 if (fmt[0] =3D=3D 'M' && i !=3D 5) - *p++ =3D separator; + pr_char(out, separator); } - *p =3D '\0'; - - string_nocheck(out, mac_addr, spec); } =20 static noinline_for_stack @@ -2270,7 +2265,8 @@ void pointer(struct printbuf *out, const char *fmt, case 'm': /* Contiguous: 000102030405 */ /* [mM]F (FDDI) */ /* [mM]R (Reverse order; Bluetooth) */ - return mac_address_string(out, ptr, spec, fmt); + mac_address_string(out, ptr, fmt); + return do_width_precision(out, prev_pos, spec); case 'I': /* Formatted IP supported * 4: 1.2.3.4 * 6: 0001:0203:...:0708 --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0BB22C433EF for ; Thu, 19 May 2022 17:27:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242781AbiESR1O (ORCPT ); Thu, 19 May 2022 13:27:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242645AbiESRZY (ORCPT ); Thu, 19 May 2022 13:25:24 -0400 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B8E6A5014; Thu, 19 May 2022 10:25:17 -0700 (PDT) Received: by mail-pl1-x631.google.com with SMTP id i8so5361723plr.13; Thu, 19 May 2022 10:25:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=gy2+5rtVyQSQ3X+3Wrn/m9PvaQt4Dx/XsZuXj5gNKrU=; b=B+r49MDVnM5d0FiWcFCC+mfxqj5H0MKA3XMEXVUTRNEHGFprEwcpGiGffDByit/Ai+ jBZ9lsrNPI2UQ5kGuaSo+Z2WQDfmbB6M+wGNoCQMSg6dmUr3T0b2Jbf+OcyHARoBHazw ObAJwwNC+hFSnPi9KlC047SwXWAGFEmSuajobWNYqrN6KoapKKu9KDJy0cwtR7L0C7ko J9vX5Usp6Z8AVWgYa2sb2uwG4cHP0xKRktevbD5cn+7iw5ta3qXqeHIVFTfHMEAGhmDf SLtSHPtudWujNfVnNb+UJirItZ2XwS5nCHrwbXQuAbe3vu/YGbwnmMIYwuK+vgzKEDWG 9O6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gy2+5rtVyQSQ3X+3Wrn/m9PvaQt4Dx/XsZuXj5gNKrU=; b=SoHV4IgvCvBq6vGuOh0v7TawFwRy8GQlUWcYivNAIAPCktsHl2kMk3mkGJM0iRDihf GyHgCARLKh+DZBPtBdaDPdx9DwiQdQi4aJ0JMZZVApPEOszPbjrGTU+rfWBdvUUgMmfd lirpQWJ7h9hLhK5MJnXOnNNFjLyP0kO1XEntJfXrFK0M7Sp7NzU8doQla3y0rsJXLm9e yXZzlyThoB3bbUJ7SChBP2S7OTmYqZm3CTi1dpiMsgT6W0gYaTB4guZYF8z9wGPdPlGJ cuRCsxmZ/KQ4sKYhPuv0+WyNWSbv3qybSaZPj8oH7FYF1252I3sn2j/kxIk/9HPO8Hu8 yxNg== X-Gm-Message-State: AOAM5311LNAKi3iSUZI3bFxonsxAszEfsCoK7V3EVonTduP/14Sz339y tG0NAl1l3GG+y8VjxF7BGCy88T6ksqDh X-Google-Smtp-Source: ABdhPJw4dVRMKUuhKQxwg42jFQsCieCMJQrREs9viGoJuAg8nY1RWFtqTLQb4iB0WD43n5V4Ac29kg== X-Received: by 2002:a17:902:a583:b0:15d:197b:9259 with SMTP id az3-20020a170902a58300b0015d197b9259mr5601285plb.51.1652981116633; Thu, 19 May 2022 10:25:16 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.25.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:25:15 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 18/28] vsprintf: time_and_date() no longer takes printf_spec Date: Thu, 19 May 2022 13:24:11 -0400 Message-Id: <20220519172421.162394-19-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" We're attempting to consolidate printf_spec and format string handling in the top level vpr_buf(), this changes time_and_date() to not take printf_spec. Signed-off-by: Kent Overstreet --- lib/vsprintf.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 76947e0d30..837a3f967e 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -1735,14 +1735,14 @@ void time_str(struct printbuf *out, const struct rt= c_time *tm, bool r) =20 static noinline_for_stack void rtc_str(struct printbuf *out, const struct rtc_time *tm, - struct printf_spec spec, const char *fmt) + const char *fmt) { bool have_t =3D true, have_d =3D true; bool raw =3D false, iso8601_separator =3D true; bool found =3D true; int count =3D 2; =20 - if (check_pointer_spec(out, tm, spec)) + if (check_pointer(out, tm)) return; =20 switch (fmt[count]) { @@ -1780,7 +1780,7 @@ void rtc_str(struct printbuf *out, const struct rtc_t= ime *tm, =20 static noinline_for_stack void time64_str(struct printbuf *out, const time64_t time, - struct printf_spec spec, const char *fmt) + const char *fmt) { struct rtc_time rtc_time; struct tm tm; @@ -1798,21 +1798,20 @@ void time64_str(struct printbuf *out, const time64_= t time, =20 rtc_time.tm_isdst =3D 0; =20 - rtc_str(out, &rtc_time, spec, fmt); + rtc_str(out, &rtc_time, fmt); } =20 static noinline_for_stack -void time_and_date(struct printbuf *out, - void *ptr, struct printf_spec spec, +void time_and_date(struct printbuf *out, void *ptr, const char *fmt) { switch (fmt[1]) { case 'R': - return rtc_str(out, (const struct rtc_time *)ptr, spec, fmt); + return rtc_str(out, (const struct rtc_time *)ptr, fmt); case 'T': - return time64_str(out, *(const time64_t *)ptr, spec, fmt); + return time64_str(out, *(const time64_t *)ptr, fmt); default: - return error_string_spec(out, "(%pt?)", spec); + return error_string(out, "(%pt?)"); } } =20 @@ -2300,7 +2299,8 @@ void pointer(struct printbuf *out, const char *fmt, dentry_name(out, ptr, fmt); return do_width_precision(out, prev_pos, spec); case 't': - return time_and_date(out, ptr, spec, fmt); + time_and_date(out, ptr, fmt); + return do_width_precision(out, prev_pos, spec); case 'C': return clock(out, ptr, spec, fmt); case 'D': --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2EECDC433EF for ; Thu, 19 May 2022 17:28:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240598AbiESR2Z (ORCPT ); Thu, 19 May 2022 13:28:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242708AbiESRZY (ORCPT ); Thu, 19 May 2022 13:25:24 -0400 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2917EAFB18; Thu, 19 May 2022 10:25:20 -0700 (PDT) Received: by mail-pl1-x62c.google.com with SMTP id d22so5362505plr.9; Thu, 19 May 2022 10:25:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zZ5b0xdEpn+CBBGAkA6i9eo0UnhdgnmaKiuhK5IzIAE=; b=nnzeicYYieFIceMJFH+7n68Pgzl2q2bBoiF9O1WjNCu5cSEMr+10//1MNuypvgaVM1 /bZ22uIrgQ71QtzfO/gAWHluPbZa9OJmocvpehzINqQvPgdjwNB6Shqx96PbdUuuJQ+I sRmS8/ffi6dzUM+LzUEduSsbcDSBSUiUIxxvhrQkzwhIbKup0bbIYjsx3peaxVRSzO9O Dt2PC0l3Nqp3HxfovEHYJIl1uwN1SLXTJP1neevx0kchvbNZdeQCkUrMnDKlZXjk6uhz x1Qo+DPGi6yXQLEF/tomaBkSevvzEq9r8oObPGl1rFkH2voVtxJVUySVTiChXlXcaNE8 EcZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zZ5b0xdEpn+CBBGAkA6i9eo0UnhdgnmaKiuhK5IzIAE=; b=p0wzNbOMkQOitoa0zIKjDYjEJHhboig9uh01eQpmaVYmgMLB74BosIMYaI+BUtnuu4 qVr30USnoNEOWIP5gHepota1jgxqk+m8YYYgQuegKB9kW5oeI2sTmkjJG895EeLmkJVt LuG3W+/vncimETvYjITXG/W/2OQmmhWQJ7RzotRGSsC9TsoF0NAai+nnCHB6UV0R+o89 eq7N2idwC+7KEwBmp/F5e4Y5k1En1L1vfZtS22Ygy5TOlFPcXn6sSoA8BDAxu4zyTByq 6N1fz8S0PCxA3OAWNM64jEJF85UJKLhJcyJKjEVMOJbudDM4kf6RKH7l9fUL4BihJHB5 iKKA== X-Gm-Message-State: AOAM532kdlOEg/lqTs/AXR/V7Dw4dhdrZ1NczGA6odVfFlTLGM8SHa7u f/v7mKvhoqEmSwimuSPhFLqJfDoJx+r0 X-Google-Smtp-Source: ABdhPJzoWpILxPY/FtcHe7auDf4GKTb4QiQq9qKUQETSfuC0bEz6CKeeIQzjc++0XSfsmCG5aefFGw== X-Received: by 2002:a17:90a:a384:b0:1dc:a407:b5ac with SMTP id x4-20020a17090aa38400b001dca407b5acmr6150964pjp.11.1652981119128; Thu, 19 May 2022 10:25:19 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.25.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:25:18 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 19/28] vsprintf: flags_string() no longer takes printf_spec Date: Thu, 19 May 2022 13:24:12 -0400 Message-Id: <20220519172421.162394-20-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" We're attempting to consolidate printf_spec and format string handling in the top level vpr_buf(), this changes time_and_date() to not take printf_spec. Signed-off-by: Kent Overstreet --- lib/vsprintf.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 837a3f967e..971cf17d33 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -1916,12 +1916,12 @@ void format_page_flags(struct printbuf *out, unsign= ed long flags) =20 static noinline_for_stack void flags_string(struct printbuf *out, void *flags_ptr, - struct printf_spec spec, const char *fmt) + const char *fmt) { unsigned long flags; const struct trace_print_flags *names; =20 - if (check_pointer_spec(out, flags_ptr, spec)) + if (check_pointer(out, flags_ptr)) return; =20 switch (fmt[1]) { @@ -1936,7 +1936,7 @@ void flags_string(struct printbuf *out, void *flags_p= tr, names =3D gfpflag_names; break; default: - return error_string_spec(out, "(%pG?)", spec); + return error_string(out, "(%pG?)"); } =20 return format_flags(out, flags, names); @@ -2313,7 +2313,8 @@ void pointer(struct printbuf *out, const char *fmt, #endif =20 case 'G': - return flags_string(out, ptr, spec, fmt); + flags_string(out, ptr, fmt); + return do_width_precision(out, prev_pos, spec); case 'O': return device_node_string(out, ptr, spec, fmt + 1); case 'f': --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AA8F6C433F5 for ; Thu, 19 May 2022 17:27:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242789AbiESR1X (ORCPT ); Thu, 19 May 2022 13:27:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242721AbiESRZf (ORCPT ); Thu, 19 May 2022 13:25:35 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64F61B040B; Thu, 19 May 2022 10:25:22 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id f10so5899824pjs.3; Thu, 19 May 2022 10:25:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IDtwsLYmJX2VI92KrqMyeH8WST984GDnjSHL6+LrCvw=; b=AGhYGSWRQ9YX9HgmiJ/WyOoO/8SiqbWStOyWHFfhAnfYEPlOYLB/n97gekxJrcvaIj jFjB5oK7lXEt1AZAYEFXJAkx4LfXzQUANPm3ZwcYrA7ovTgvBN6TaBUJyL8cXGd1ic8N cl0vm3vi0N2CIw1TibZPw8z6fY1GqJdxfCM8wXI1Ny+EHoxoBMfrqfeMJlxCUbbLyboT 7ytaRjOzKZ05/49H2l08CZXmG7KQAhdHWYEwo9fCQHNRb47Vhv5Z1gfcOzWxgBHXeyuP ZJ11VuM+lzskUFGdDC9ZKMifrhGNXIl0a8q+g6PVILguFrvz1eOM3Cza/MSAMsUkVIPi nrEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IDtwsLYmJX2VI92KrqMyeH8WST984GDnjSHL6+LrCvw=; b=pPZ2jDaIeh6eS8NTBmEN0wLLXfsmAdApYC0td5RIPbSyDbRWtMBs/Pzn8RWEQFPwCn /bXvtZI2r2U6DxF4ZSt9T3N1PUIFHkEhUhqq86K8hMlki1uqbOzafglRDPZnRofLTJhs xLkY7cJQ4pvfYY8s3yYKfsJGNbyRZ0oe3auSNNxAR9Sgodhsm2bedyNb1F1Ka/q+WH/V Tdv/d9S1RFKQf8zI7KIx56eDLje6u2iIGn70ObknWjhNaXYKpG3YKuD81yfpXPYouPwS DMrt+az4yv9sogWEUytOL6LtBt0QWenpcxVO1Kw2xWo8OPSIKWqZ7f9NwlGig5/f3Bfp pwNg== X-Gm-Message-State: AOAM531Uqc5RmkLzd2rJ0Kc42zsg/zJeCdnnzL31lAXPAdZgNfLsnFEm if7UFrzRih6X9hXih8A1+HhC4kcaR5HZ X-Google-Smtp-Source: ABdhPJwCQnlAgwYPFM1FAPh7NCigi4C/s5gvY/TMwe/eBeT/sE/vNfCX2jPPmVLXlqgPdCFItzC3oQ== X-Received: by 2002:a17:90b:4a01:b0:1dc:67b8:983f with SMTP id kk1-20020a17090b4a0100b001dc67b8983fmr6207580pjb.1.1652981121161; Thu, 19 May 2022 10:25:21 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.25.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:25:20 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 20/28] vsprintf: Refactor device_node_string, fwnode_string Date: Thu, 19 May 2022 13:24:13 -0400 Message-Id: <20220519172421.162394-21-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" - eliminate on-stack buffer in device_node_string - eliminate unnecessary uses of printf_spec, lift format string precision/field width to pointer() Signed-off-by: Kent Overstreet --- lib/vsprintf.c | 75 ++++++++++++++++++++------------------------------ 1 file changed, 30 insertions(+), 45 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 971cf17d33..ef8b06a25a 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -1962,25 +1962,20 @@ void fwnode_full_name_string(struct printbuf *out, =20 static noinline_for_stack void device_node_string(struct printbuf *out, struct device_node *dn, - struct printf_spec spec, const char *fmt) + const char *fmt) { - char tbuf[sizeof("xxxx") + 1]; const char *p; int ret; - unsigned start =3D out->pos; struct property *prop; bool has_mult, pass; =20 - struct printf_spec str_spec =3D spec; - str_spec.field_width =3D -1; - if (fmt[0] !=3D 'F') - return error_string_spec(out, "(%pO?)", spec); + return error_string(out, "(%pO?)"); =20 if (!IS_ENABLED(CONFIG_OF)) - return error_string_spec(out, "(%pOF?)", spec); + return error_string(out, "(%pOF?)"); =20 - if (check_pointer_spec(out, dn, spec)) + if (check_pointer(out, dn)) return; =20 /* simple case without anything any more format specifiers */ @@ -1989,51 +1984,48 @@ void device_node_string(struct printbuf *out, struc= t device_node *dn, fmt =3D "f"; =20 for (pass =3D false; strspn(fmt,"fnpPFcC"); fmt++, pass =3D true) { - int precision; if (pass) - __pr_char(out, ':'); + pr_char(out, ':'); =20 switch (*fmt) { case 'f': /* full_name */ fwnode_full_name_string(out, of_fwnode_handle(dn)); break; - case 'n': /* name */ - p =3D fwnode_get_name(of_fwnode_handle(dn)); - precision =3D str_spec.precision; - str_spec.precision =3D strchrnul(p, '@') - p; - string_spec(out, p, str_spec); - str_spec.precision =3D precision; + case 'n': { /* name */ + const char *name =3D fwnode_get_name(of_fwnode_handle(dn)); + unsigned len =3D strchrnul(name, '@') - name; + + pr_bytes(out, name, len); break; + } case 'p': /* phandle */ - pr_u64(out, (unsigned int)dn->phandle); + pr_u64(out, dn->phandle); break; case 'P': /* path-spec */ p =3D fwnode_get_name(of_fwnode_handle(dn)); if (!p[1]) p =3D "/"; - string_spec(out, p, str_spec); + string(out, p); break; case 'F': /* flags */ - tbuf[0] =3D of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-'; - tbuf[1] =3D of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-'; - tbuf[2] =3D of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-'; - tbuf[3] =3D of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-'; - tbuf[4] =3D 0; - string_nocheck(out, tbuf, str_spec); + pr_char(out, of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-'); + pr_char(out, of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-'); + pr_char(out, of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-'); + pr_char(out, of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-'); break; case 'c': /* major compatible string_spec */ ret =3D of_property_read_string(dn, "compatible", &p); if (!ret) - string_spec(out, p, str_spec); + string(out, p); break; case 'C': /* full compatible string_spec */ has_mult =3D false; of_property_for_each_string(dn, "compatible", prop, p) { if (has_mult) - string_nocheck(out, ",", str_spec); - string_nocheck(out, "\"", str_spec); - string_spec(out, p, str_spec); - string_nocheck(out, "\"", str_spec); + pr_char(out, ','); + pr_char(out, '\"'); + string(out, p); + pr_char(out, '\"'); =20 has_mult =3D true; } @@ -2042,39 +2034,30 @@ void device_node_string(struct printbuf *out, struc= t device_node *dn, break; } } - - widen_string(out, out->pos - start, spec); } =20 static noinline_for_stack void fwnode_string(struct printbuf *out, struct fwnode_handle *fwnode, - struct printf_spec spec, const char *fmt) + const char *fmt) { - struct printf_spec str_spec =3D spec; - unsigned start =3D out->pos; - - str_spec.field_width =3D -1; - if (*fmt !=3D 'w') - return error_string_spec(out, "(%pf?)", spec); + return error_string(out, "(%pf?)"); =20 - if (check_pointer_spec(out, fwnode, spec)) + if (check_pointer(out, fwnode)) return; =20 fmt++; =20 switch (*fmt) { case 'P': /* name */ - string_spec(out, fwnode_get_name(fwnode), str_spec); + string(out, fwnode_get_name(fwnode)); break; case 'f': /* full_name */ default: fwnode_full_name_string(out, fwnode); break; } - - widen_string(out, out->pos - start, spec); } =20 /* Disable pointer hashing if requested */ @@ -2316,9 +2299,11 @@ void pointer(struct printbuf *out, const char *fmt, flags_string(out, ptr, fmt); return do_width_precision(out, prev_pos, spec); case 'O': - return device_node_string(out, ptr, spec, fmt + 1); + device_node_string(out, ptr, fmt + 1); + return do_width_precision(out, prev_pos, spec); case 'f': - return fwnode_string(out, ptr, spec, fmt + 1); + fwnode_string(out, ptr, fmt + 1); + return do_width_precision(out, prev_pos, spec); case 'x': return pointer_string(out, ptr, spec); case 'e': --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2E454C433EF for ; Thu, 19 May 2022 17:27:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242855AbiESR1f (ORCPT ); Thu, 19 May 2022 13:27:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242659AbiESRZf (ORCPT ); Thu, 19 May 2022 13:25:35 -0400 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 510A9A5018; Thu, 19 May 2022 10:25:24 -0700 (PDT) Received: by mail-pg1-x531.google.com with SMTP id v10so5635312pgl.11; Thu, 19 May 2022 10:25:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8HbBS6xmnZ/fOvGf3tPUK0KgU6ZNd4ATaXcG9E299js=; b=hu2Fl6JiyfeeMY+AFR65+MFrqea9eZOdmSto/bcEkiwl9CXd2SumnAeJsYDhkeubEa lIUJiSdeJVVxoxYkGUvnoXXV/9jKOvIw44mD4WXfRd6KaqbykvpQ979nceAX+HRtzXvY zyCw6OMJcDTIqfLjoBVoN8KSVFvfpfHz82kJbQQHS7ZnvAkIDdt/noG+q5+1AgqzmbK2 Mgwf5IIfH1ZLiSRibfMI4efuAXBfbz/GGfsGypDP3wSMBJ94SmvppYaS0o5U3BwmXMrX uB2ic5TIUtUXMYWatshvn8JrQOBfhT0I28St6wg+jqIXAxjbgYd+ZMWE2XRWcMFzlBrG T4nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8HbBS6xmnZ/fOvGf3tPUK0KgU6ZNd4ATaXcG9E299js=; b=ZngCKlG7pSI6YGq5xFxtiKUqu6XlHo5hrNisL0d6YbSdkwXlCqbG76kjhGtoh36/ko PZdHJWRJ10ro0sGzMJcLq9KkDkWUxpa8OpqiN85//pWGKsXHQouMDz2hMKWeBgNlKKoL fB4sGT/eoh/Fe+Ba+DuPG3kfip6wW+5twefCaoX5F+4HhlG+K7rntvRtwfY+JoIrns92 URXRM7b3Enjcl4E0AIPrnetVk0Uw8n08rGj4nbKLZTCIcPLe4Ryn90Gf8aPZxUt3vvwD ulRTNUZT0bjCOku8fEUzogMV+D23mBGGCu7yOdT/RI3aU521pTBZoXcjS9A6phpfHRNH zYkQ== X-Gm-Message-State: AOAM5339B8A8rBHF4cuxApKUfIWBk6XQZWH5Bjl78ZXNBPCRbY7/bS+A ivly9Y5f/eD7krTV7en00a9+2K0wUVQR X-Google-Smtp-Source: ABdhPJyXdzOnmsoW0cVnKRTdVC06xejnqiGQDIbIHyuHSH9ORmZRPLC4N+1pmHuFj3v8B9/YvIQumw== X-Received: by 2002:a05:6a00:1a03:b0:510:a1d9:7de0 with SMTP id g3-20020a056a001a0300b00510a1d97de0mr5581474pfv.53.1652981123175; Thu, 19 May 2022 10:25:23 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.25.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:25:22 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 21/28] vsprintf: Refactor hex_string, bitmap_string_list, bitmap_string Date: Thu, 19 May 2022 13:24:14 -0400 Message-Id: <20220519172421.162394-22-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This patch cleans up printf_spec handling: these functions only use spec.field_width and they do not interpret it in the normal way - instead it's a number of bits/bytes passed in to print, so these functions are changed to take that parameter directly. Signed-off-by: Kent Overstreet --- lib/vsprintf.c | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index ef8b06a25a..be2fa6053c 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -1132,17 +1132,22 @@ void resource_string(struct printbuf *out, struct r= esource *res, =20 static noinline_for_stack void hex_string(struct printbuf *out, const u8 *addr, - struct printf_spec spec, const char *fmt) + int len, const char *fmt) { - int len =3D 1; /* if we pass '%ph[CDN]', field width remains - negative value, fallback to the default */ char separator; =20 - if (spec.field_width =3D=3D 0) - /* nothing to print */ + /* nothing to print */ + if (len =3D=3D 0) return; =20 - if (check_pointer_spec(out, addr, spec)) + /* if we pass '%ph[CDN]', field width remains + negative value, fallback to the default */ + if (len < 0) + len =3D 1; + + len =3D min(len, 64); + + if (check_pointer(out, addr)) return; =20 switch (fmt[1]) { @@ -1160,26 +1165,21 @@ void hex_string(struct printbuf *out, const u8 *add= r, break; } =20 - if (spec.field_width > 0) - len =3D min_t(int, spec.field_width, 64); - pr_hex_bytes(out, addr, len, separator); } =20 static noinline_for_stack -void bitmap_string(struct printbuf *out, unsigned long *bitmap, - struct printf_spec spec, const char *fmt) +void bitmap_string(struct printbuf *out, unsigned long *bitmap, int nr_bit= s) { + struct printf_spec spec =3D { .flags =3D SMALL | ZEROPAD, .base =3D 16 }; const int CHUNKSZ =3D 32; - int nr_bits =3D max_t(int, spec.field_width, 0); int i, chunksz; bool first =3D true; =20 - if (check_pointer_spec(out, bitmap, spec)) - return; + nr_bits =3D max(nr_bits, 0); =20 - /* reused to print numbers */ - spec =3D (struct printf_spec){ .flags =3D SMALL | ZEROPAD, .base =3D 16 }; + if (check_pointer(out, bitmap)) + return; =20 chunksz =3D nr_bits & (CHUNKSZ - 1); if (chunksz =3D=3D 0) @@ -1208,13 +1208,14 @@ void bitmap_string(struct printbuf *out, unsigned l= ong *bitmap, =20 static noinline_for_stack void bitmap_list_string(struct printbuf *out, unsigned long *bitmap, - struct printf_spec spec, const char *fmt) + int nr_bits) { - int nr_bits =3D max_t(int, spec.field_width, 0); bool first =3D true; int rbot, rtop; =20 - if (check_pointer_spec(out, bitmap, spec)) + nr_bits =3D max(nr_bits, 0); + + if (check_pointer(out, bitmap)) return ; =20 for_each_set_bitrange(rbot, rtop, bitmap, nr_bits) { @@ -2235,13 +2236,15 @@ void pointer(struct printbuf *out, const char *fmt, resource_string(out, ptr, fmt[0] =3D=3D 'R'); return do_width_precision(out, prev_pos, spec); case 'h': - return hex_string(out, ptr, spec, fmt); + /* Uses field_width but _not_ as field size */ + return hex_string(out, ptr, spec.field_width, fmt); case 'b': + /* Uses field_width but _not_ as field size */ switch (fmt[1]) { case 'l': - return bitmap_list_string(out, ptr, spec, fmt); + return bitmap_list_string(out, ptr, spec.field_width); default: - return bitmap_string(out, ptr, spec, fmt); + return bitmap_string(out, ptr, spec.field_width); } case 'M': /* Colon separated: 00:01:02:03:04:05 */ case 'm': /* Contiguous: 000102030405 */ --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6B95AC433EF for ; Thu, 19 May 2022 17:27:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242813AbiESR1b (ORCPT ); Thu, 19 May 2022 13:27:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242742AbiESRZf (ORCPT ); Thu, 19 May 2022 13:25:35 -0400 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62372A5033; Thu, 19 May 2022 10:25:26 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id o13-20020a17090a9f8d00b001df3fc52ea7so9353278pjp.3; Thu, 19 May 2022 10:25:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HkgeacWNM0o6tNfidYESYHohydRW3WSO9rnNuCMWeRg=; b=U7k0OV5r4mJ0TuwkYHZY7iFwh3ViIBrZQI87oOkTjIb5fl9vYBG/SpTFZB3TzoImJ+ a7IdlgB0bT6Yv3HhL2eZu1nV7ybD5CJr3I5a/I5oVIamgGKoZhyqdJAJZmRzvKu7lEKi tm59BqvVMbB+85NN5tXyyH47NJIKBL9WPZ0D2MwdyABOF32IUfvGXMCTlCMagNXwM0mq qhVpIpiE7wTQzu5cDelhviCRfBo5G66ATArxWzkbJVG1isd+rssuOqZ35K58lJojO8o0 QaXFup5GzXrj40bwRYl/K+HTvNSCVED1yj5cvrMr3jzXTYH/lTAM5cEqnmfOjTyhsRUA T84g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HkgeacWNM0o6tNfidYESYHohydRW3WSO9rnNuCMWeRg=; b=n8trNINN7IcqntQd6G7dyDk5xF76EvFzF1LmsriNo+TorsORdAzoSpZ5uN4qVMitFq KRgdgj2XQ4k62Hv3i36upT2HVy37iCD1hQsDhpJXKq3WrX0h6c2+9gp2RNU+1YrmuLMG 1D8QjK9DspcivSLZXd4PHw9NxLstR80D+8B8emTl7eVtALLADd3ZsqDTZmLDJuuiiZU+ BIelQX+29G0oF5nXly7HlONcqsvWMM1nbtBzzAv3YbVcOsICmBfeMbiiyzjMOcMSCN3h 20ISj5/KRiA3H6D7NnTfBkt8ctoLEST2YDj3h1NRJW5iLmv4SjL4kNp3AbPirrzbZFsf efgg== X-Gm-Message-State: AOAM5334Tlg2U0TSllhnlNBGdQRS+Fkx2eiErll7sisdL0BkUgWUJpoU zGK0sgs9cQwB/XiAi4/P0wIdHnKLm92+ X-Google-Smtp-Source: ABdhPJw1xLSKQfSuo/sCNUUiDwCl90STmqlc/fYFr6w1tWKMlYw+Oncip0utICQcTOjERRdTjUtA1g== X-Received: by 2002:a17:90a:2809:b0:1df:35ca:2e6a with SMTP id e9-20020a17090a280900b001df35ca2e6amr6208779pjd.8.1652981125365; Thu, 19 May 2022 10:25:25 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.25.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:25:24 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 22/28] Input/joystick/analog: Convert from seq_buf -> printbuf Date: Thu, 19 May 2022 13:24:15 -0400 Message-Id: <20220519172421.162394-23-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" seq_buf is being deprecated, this converts to printbuf which is similar but heap allocates the string buffer. This means we have to consider memory allocation context & failure: Here we're in device initialization so GFP_KERNEL should be fine, and also as we're in device initialization returning -ENOMEM is fine. Signed-off-by: Kent Overstreet --- drivers/input/joystick/analog.c | 37 ++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/drivers/input/joystick/analog.c b/drivers/input/joystick/analo= g.c index 3088c5b829..72e1e30d19 100644 --- a/drivers/input/joystick/analog.c +++ b/drivers/input/joystick/analog.c @@ -19,7 +19,7 @@ #include #include #include -#include +#include #include #include =20 @@ -337,26 +337,32 @@ static void analog_calibrate_timer(struct analog_port= *port) * analog_name() constructs a name for an analog joystick. */ =20 -static void analog_name(struct analog *analog) +static int analog_name(struct analog *analog) { - struct seq_buf s; + struct printbuf buf =3D PRINTBUF; + int ret =3D 0; =20 - seq_buf_init(&s, analog->name, sizeof(analog->name)); - seq_buf_printf(&s, "Analog %d-axis %d-button", - hweight8(analog->mask & ANALOG_AXES_STD), - hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTN= S_CHF) * 2 + - hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALO= G_HBTN_CHF) * 4); + pr_buf(&buf, "Analog %d-axis %d-button", + hweight8(analog->mask & ANALOG_AXES_STD), + hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALO= G_BTNS_CHF) * 2 + + hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & = ANALOG_HBTN_CHF) * 4); =20 if (analog->mask & ANALOG_HATS_ALL) - seq_buf_printf(&s, " %d-hat", - hweight16(analog->mask & ANALOG_HATS_ALL)); + pr_buf(&buf, " %d-hat", + hweight16(analog->mask & ANALOG_HATS_ALL)); =20 if (analog->mask & ANALOG_HAT_FCS) - seq_buf_printf(&s, " FCS"); + pr_buf(&buf, " FCS"); if (analog->mask & ANALOG_ANY_CHF) - seq_buf_printf(&s, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF"); + pr_buf(&buf, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF"); =20 - seq_buf_printf(&s, (analog->mask & ANALOG_GAMEPAD) ? " gamepad" : " joyst= ick"); + pr_buf(&buf, (analog->mask & ANALOG_GAMEPAD) ? " gamepad" : " joystick"); + + ret =3D buf.allocation_failure ? -ENOMEM : 0; + if (!ret) + strlcpy(analog->name, buf.buf, sizeof(analog->name)); + printbuf_exit(&buf); + return ret; } =20 /* @@ -369,7 +375,10 @@ static int analog_init_device(struct analog_port *port= , struct analog *analog, i int i, j, t, v, w, x, y, z; int error; =20 - analog_name(analog); + error =3D analog_name(analog); + if (error) + return error; + snprintf(analog->phys, sizeof(analog->phys), "%s/input%d", port->gameport->phys, index); analog->buttons =3D (analog->mask & ANALOG_GAMEPAD) ? analog_pad_btn : an= alog_joy_btn; --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EA254C433F5 for ; Thu, 19 May 2022 17:28:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242649AbiESR2U (ORCPT ); Thu, 19 May 2022 13:28:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242748AbiESRZg (ORCPT ); Thu, 19 May 2022 13:25:36 -0400 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C33ADC1EC8; Thu, 19 May 2022 10:25:30 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id qe3-20020a17090b4f8300b001dc24e4da73so6418786pjb.1; Thu, 19 May 2022 10:25:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7B0kqteeN1eyAor+oWwIzsi+4oWzLA5TO8L9iLe1/34=; b=nPqtZm7p2v7hAPXvOp5FvLWPKwjOUfVcVl7nNXUblqG3FYpYJbXYQvFLWKJ+8w5j31 1IJZZ3qknsRGl55pNTfXFgIY9oUbAfTmWFE/0OWuKQAw2Eoxj9PyaiXFDFPkDOpGMmlM MqNM5pGOia/fB48No3VSI9urtCFGC10feuW4/a83Ureud/jN2ukq2kHwSpxzg6Whu1PZ leIdKVqjV37XYm3FYpkMrR6Gp6fNidApn4YH8CemucGJ2jgfORIXGEwuKeEneYgvnQn2 h33A+2n6Zg1/VxTfEtNPQaBuBRObTh+LTk0RK3JzC88+0it8dFakz9u+iNlGyF1y3zFN pxzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7B0kqteeN1eyAor+oWwIzsi+4oWzLA5TO8L9iLe1/34=; b=h0bYX4D3DWlGebEAW3+sTCWODRYx/nGl1Otu6DM0sNFEpyuD+3rG+MdtOLNSXdh/JG hJpuXQYQglXhWsDUg0AuhUcN5gbWHPU3qUaMChLaKk2SsuyLNgCHiLCCiejI/XokOqjh 6bdeOXhonAakV/KCXt5hmNAR4enfESdcBaVX99E0mexfLQv7P3Qmf5yKvZiB20GVZBwT YTxCmOhvfi7K9MEjxvB7dfTZXLg5ddmvyPVI7hZJGlsLgwHSoK9d/f73Gzu4Rixyx3xJ c46zaZjWaOkjZrNMCAXSD8ZjKdT8GxzgtFRa+2tdiRINV5eY4dgVtsBjO2EA2fbckRU3 gQPA== X-Gm-Message-State: AOAM531u5S65K6cjp7mrW15729S0MHLENGcS5g+lzehA3SVwQH50s6SI A+AbHZELg8Wwv1ohw4q9tvRIlNqtlLQm X-Google-Smtp-Source: ABdhPJyk4ae2VWBS31RHITrOF+FdzGDA2lzJY8LITE45Phy9kfhfqDOAHepVXVuBLVNeymEnXEq8Qg== X-Received: by 2002:a17:903:2609:b0:161:58c6:77e7 with SMTP id jd9-20020a170903260900b0016158c677e7mr5532119plb.36.1652981129665; Thu, 19 May 2022 10:25:29 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.25.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:25:28 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 23/28] mm/memcontrol.c: Convert to printbuf Date: Thu, 19 May 2022 13:24:16 -0400 Message-Id: <20220519172421.162394-24-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This converts memory_stat_format() from seq_buf to printbuf. Printbuf is simalar to seq_buf except that it heap allocates the string buffer: here, we were already heap allocating the buffer with kmalloc() so the conversion is trivial. Signed-off-by: Kent Overstreet --- mm/memcontrol.c | 68 ++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 36e9f38c91..4cb0b7bc1c 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -61,7 +61,7 @@ #include #include #include -#include +#include #include "internal.h" #include #include @@ -1436,13 +1436,9 @@ static inline unsigned long memcg_page_state_output(= struct mem_cgroup *memcg, =20 static char *memory_stat_format(struct mem_cgroup *memcg) { - struct seq_buf s; + struct printbuf buf =3D PRINTBUF; int i; =20 - seq_buf_init(&s, kmalloc(PAGE_SIZE, GFP_KERNEL), PAGE_SIZE); - if (!s.buffer) - return NULL; - /* * Provide statistics on the state of the memory subsystem as * well as cumulative event counters that show past behavior. @@ -1459,49 +1455,51 @@ static char *memory_stat_format(struct mem_cgroup *= memcg) u64 size; =20 size =3D memcg_page_state_output(memcg, memory_stats[i].idx); - seq_buf_printf(&s, "%s %llu\n", memory_stats[i].name, size); + pr_buf(&buf, "%s %llu\n", memory_stats[i].name, size); =20 if (unlikely(memory_stats[i].idx =3D=3D NR_SLAB_UNRECLAIMABLE_B)) { size +=3D memcg_page_state_output(memcg, NR_SLAB_RECLAIMABLE_B); - seq_buf_printf(&s, "slab %llu\n", size); + pr_buf(&buf, "slab %llu\n", size); } } =20 /* Accumulated memory events */ =20 - seq_buf_printf(&s, "%s %lu\n", vm_event_name(PGFAULT), - memcg_events(memcg, PGFAULT)); - seq_buf_printf(&s, "%s %lu\n", vm_event_name(PGMAJFAULT), - memcg_events(memcg, PGMAJFAULT)); - seq_buf_printf(&s, "%s %lu\n", vm_event_name(PGREFILL), - memcg_events(memcg, PGREFILL)); - seq_buf_printf(&s, "pgscan %lu\n", - memcg_events(memcg, PGSCAN_KSWAPD) + - memcg_events(memcg, PGSCAN_DIRECT)); - seq_buf_printf(&s, "pgsteal %lu\n", - memcg_events(memcg, PGSTEAL_KSWAPD) + - memcg_events(memcg, PGSTEAL_DIRECT)); - seq_buf_printf(&s, "%s %lu\n", vm_event_name(PGACTIVATE), - memcg_events(memcg, PGACTIVATE)); - seq_buf_printf(&s, "%s %lu\n", vm_event_name(PGDEACTIVATE), - memcg_events(memcg, PGDEACTIVATE)); - seq_buf_printf(&s, "%s %lu\n", vm_event_name(PGLAZYFREE), - memcg_events(memcg, PGLAZYFREE)); - seq_buf_printf(&s, "%s %lu\n", vm_event_name(PGLAZYFREED), - memcg_events(memcg, PGLAZYFREED)); + pr_buf(&buf, "%s %lu\n", vm_event_name(PGFAULT), + memcg_events(memcg, PGFAULT)); + pr_buf(&buf, "%s %lu\n", vm_event_name(PGMAJFAULT), + memcg_events(memcg, PGMAJFAULT)); + pr_buf(&buf, "%s %lu\n", vm_event_name(PGREFILL), + memcg_events(memcg, PGREFILL)); + pr_buf(&buf, "pgscan %lu\n", + memcg_events(memcg, PGSCAN_KSWAPD) + + memcg_events(memcg, PGSCAN_DIRECT)); + pr_buf(&buf, "pgsteal %lu\n", + memcg_events(memcg, PGSTEAL_KSWAPD) + + memcg_events(memcg, PGSTEAL_DIRECT)); + pr_buf(&buf, "%s %lu\n", vm_event_name(PGACTIVATE), + memcg_events(memcg, PGACTIVATE)); + pr_buf(&buf, "%s %lu\n", vm_event_name(PGDEACTIVATE), + memcg_events(memcg, PGDEACTIVATE)); + pr_buf(&buf, "%s %lu\n", vm_event_name(PGLAZYFREE), + memcg_events(memcg, PGLAZYFREE)); + pr_buf(&buf, "%s %lu\n", vm_event_name(PGLAZYFREED), + memcg_events(memcg, PGLAZYFREED)); =20 #ifdef CONFIG_TRANSPARENT_HUGEPAGE - seq_buf_printf(&s, "%s %lu\n", vm_event_name(THP_FAULT_ALLOC), - memcg_events(memcg, THP_FAULT_ALLOC)); - seq_buf_printf(&s, "%s %lu\n", vm_event_name(THP_COLLAPSE_ALLOC), - memcg_events(memcg, THP_COLLAPSE_ALLOC)); + pr_buf(&buf, "%s %lu\n", vm_event_name(THP_FAULT_ALLOC), + memcg_events(memcg, THP_FAULT_ALLOC)); + pr_buf(&buf, "%s %lu\n", vm_event_name(THP_COLLAPSE_ALLOC), + memcg_events(memcg, THP_COLLAPSE_ALLOC)); #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ =20 - /* The above should easily fit into one page */ - WARN_ON_ONCE(seq_buf_has_overflowed(&s)); + if (buf.allocation_failure) { + printbuf_exit(&buf); + return NULL; + } =20 - return s.buffer; + return buf.buf; } =20 #define K(x) ((x) << (PAGE_SHIFT-10)) --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 89730C433F5 for ; Thu, 19 May 2022 17:28:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240725AbiESR2K (ORCPT ); Thu, 19 May 2022 13:28:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242783AbiESRZo (ORCPT ); Thu, 19 May 2022 13:25:44 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A6CE5C9ECE; Thu, 19 May 2022 10:25:32 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id i24so5706065pfa.7; Thu, 19 May 2022 10:25:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=M4VpQ9FA/yOtZ7kv3MA0tILWqoI98zOrKzzcPQLN1bE=; b=VS336rDNF126aiqs0zDhNVmRlLhtdgOCtQzOvqIe2Yzq4a3SGLnU0Wa5V7UjGDyzr8 HxiGB68x6vSj4roCHekfpfutPa3ii3iKfD5PkDTOztas057DLWq5mqhTKXCOEM4R/nUs 3lLvfn3LhzinKCo56cWZvPp5dPpaRJlN3OdgsJC8bpu/VUxPJbTgWFsFj5neYnMLUiPi cEaeRm3mSfKibmHRgk9FvrOuIYVE2bS1+Cy7niekwndpz+L5ARq1Q09uI4m1P8dWNNzx mBSSZvvkdDNtvD3XJdi/owL8ae1dB+A/1s1tOtvvKYixCm1VFyjKk2S0Fm/w04gx9dQc YMLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=M4VpQ9FA/yOtZ7kv3MA0tILWqoI98zOrKzzcPQLN1bE=; b=VtbZZu70W3MCBATFPcq8ze2rnsAoifXvnwW8LOa0aEnvIKMLHac6X6mLsqX2f8z7Zg Dv7NOkuSr+lHmbO1N8bRbyW4SzvnfXUg7GWBaD+Uf5NOubAYaCCr8uZqUuz4zXJkHi4R krA+R1TFj5A6D7t5QSYelKlFVSfM90Aj5VY0u22uT8X0pOFX2qk7smQe7ZVzHF6bApFO RerqQBLsubsat6FA5HGD2hNHKlXaxlAqJBPwNssaZEr3uEy3mgywG6IzyrP55oEnpc+/ yhp2v6e+/LxF+QqGrs38M8GtAYLdM4ahswO03bWIVFKKNt+mqB0nhh3hF9OsfX3Ve+ex 1kiA== X-Gm-Message-State: AOAM532HAa1oFMo4Q+3J6f1U/pJ3Z2Kmvhg05ev/tJdz9rxkSla6hgIk 231p+xJVKeBgUIr19qSEERT8kTewYQFS X-Google-Smtp-Source: ABdhPJyHPF4eVFHphoTFrwK/26y6X6rTq/0g6vrr7JSEA1842SO8X+Q8rU+gTBd5uMonppR1gwCSUw== X-Received: by 2002:a63:5fcf:0:b0:3f6:298d:e2ea with SMTP id t198-20020a635fcf000000b003f6298de2eamr4902749pgb.561.1652981131850; Thu, 19 May 2022 10:25:31 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.25.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:25:31 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org Subject: [PATCH v2 24/28] clk: tegra: bpmp: Convert to printbuf Date: Thu, 19 May 2022 13:24:17 -0400 Message-Id: <20220519172421.162394-25-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This converts from seq_buf to printbuf, which is similar but heap allocates the string buffer. Previously in this code the string buffer was allocated on the stack; this means we've added a new potential memory allocation failure. This is fine though since it's only for a dev_printk() message. Memory allocation context: printbuf doesn't take gfp flags, instead we prefer the new memalloc_no*_(save|restore) interfaces to be used. Here the surrounding code is already allocating with GFP_KERNEL, so everything is fine. Signed-off-by: Kent Overstreet --- drivers/clk/tegra/clk-bpmp.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/drivers/clk/tegra/clk-bpmp.c b/drivers/clk/tegra/clk-bpmp.c index 6ecf18f71c..77a8c47806 100644 --- a/drivers/clk/tegra/clk-bpmp.c +++ b/drivers/clk/tegra/clk-bpmp.c @@ -5,7 +5,7 @@ =20 #include #include -#include +#include #include =20 #include @@ -360,39 +360,38 @@ static void tegra_bpmp_clk_info_dump(struct tegra_bpm= p *bpmp, const struct tegra_bpmp_clk_info *info) { const char *prefix =3D ""; - struct seq_buf buf; + struct printbuf buf =3D PRINTBUF; unsigned int i; - char flags[64]; - - seq_buf_init(&buf, flags, sizeof(flags)); =20 if (info->flags) - seq_buf_printf(&buf, "("); + pr_buf(&buf, "("); =20 if (info->flags & TEGRA_BPMP_CLK_HAS_MUX) { - seq_buf_printf(&buf, "%smux", prefix); + pr_buf(&buf, "%smux", prefix); prefix =3D ", "; } =20 if ((info->flags & TEGRA_BPMP_CLK_HAS_SET_RATE) =3D=3D 0) { - seq_buf_printf(&buf, "%sfixed", prefix); + pr_buf(&buf, "%sfixed", prefix); prefix =3D ", "; } =20 if (info->flags & TEGRA_BPMP_CLK_IS_ROOT) { - seq_buf_printf(&buf, "%sroot", prefix); + pr_buf(&buf, "%sroot", prefix); prefix =3D ", "; } =20 if (info->flags) - seq_buf_printf(&buf, ")"); + pr_buf(&buf, ")"); =20 dev_printk(level, bpmp->dev, "%03u: %s\n", info->id, info->name); - dev_printk(level, bpmp->dev, " flags: %lx %s\n", info->flags, flags); + dev_printk(level, bpmp->dev, " flags: %lx %s\n", info->flags, printbuf_s= tr(&buf)); dev_printk(level, bpmp->dev, " parents: %u\n", info->num_parents); =20 for (i =3D 0; i < info->num_parents; i++) dev_printk(level, bpmp->dev, " %03u\n", info->parents[i]); + + printbuf_exit(&buf); } =20 static int tegra_bpmp_probe_clocks(struct tegra_bpmp *bpmp, --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8EE41C433F5 for ; Thu, 19 May 2022 17:27:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242872AbiESR1n (ORCPT ); Thu, 19 May 2022 13:27:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242680AbiESRZw (ORCPT ); Thu, 19 May 2022 13:25:52 -0400 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34DB5AF31A; Thu, 19 May 2022 10:25:35 -0700 (PDT) Received: by mail-pj1-x1032.google.com with SMTP id w17-20020a17090a529100b001db302efed6so5828315pjh.4; Thu, 19 May 2022 10:25:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2QRd1Hj7z8PQ6OGAWnpJxYHyy9vDcMOmiRfmbwhyjgQ=; b=hRwvBgiXo+VzZn4+ZMIuphIGy3cRF1TZwhoAN7s4erqo0CFUk1qb6ydVp88d+2ipZi yJUJi0Vh+xqciOv/aQqybtbFA0SnHgW11kIpK7xCdSLq/JtOf56mzXpce/OevP5i8ryj RpxcKm3CLN2X/5r7tX2qLHOrtwgaScLaFuzYtEEMVLbqgiN74TV36cmO3hF12CqtCQ9C DaNjTFnJRzNK8zou/DzS9HANilhYaiwS4DrM4QaSv9VKBFLV3uzkMAxedajyr7YJS54O mz5s5WeLD5t36bFXc3CHmj6/16zZj8DfbVv8OsRa3DOkYQT1g2sPgZL2y5nd/1mksJZS GRUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2QRd1Hj7z8PQ6OGAWnpJxYHyy9vDcMOmiRfmbwhyjgQ=; b=Nx+ghqadCXiMw3XLPVVFJeheBcao1If5nqDgjCqc3OKTM3zTAmOm96KxukoYHzbwbv Fl9xRj2OGpnpRrvxlPIT8Dx9YWW/k7g840AI/wN3ksaAZHegWMIbUPOw6zspe+nJFaQ0 U4rKeJ/Ica4hmuFGYlnCONLV8R/PFsHZg91nk6FdFgVbcGD2qg1VJ7e+gVsubj7cXyv/ 36+WMVmPc8iSVf2wXk29TRn+TlKCTfgUSl2a54Diq5DAls4xa051vFvShvxYF1AWXLVI 7wHqU3D9hbmJ7olRwKDi/Yd/GCAcGM2kdnRazejvAXGWGNZGHbVmQcGIcAiD3KRRfRa2 e6VQ== X-Gm-Message-State: AOAM532bWHwubfXUfCLTqe1+fCflafci9W2/ZgmsUvXN2rlwO11LjkTe xmZ3XBTZHnYb6tR59ST5fuGBCx5BPodr X-Google-Smtp-Source: ABdhPJwilFeifO0CVAPkBmNZUYBAAIobtr791UrG2yrXWCxaxjHkGeXGvgtvvCY54ifcmkoBoEresA== X-Received: by 2002:a17:90b:3e8b:b0:1dc:e471:1a69 with SMTP id rj11-20020a17090b3e8b00b001dce4711a69mr6878682pjb.60.1652981134105; Thu, 19 May 2022 10:25:34 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.25.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:25:33 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org, Dan Williams , Dave Hansen , nvdimm@lists.linux.dev Subject: [PATCH v2 25/28] tools/testing/nvdimm: Convert to printbuf Date: Thu, 19 May 2022 13:24:18 -0400 Message-Id: <20220519172421.162394-26-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This converts from seq_buf to printbuf. Here we're using printbuf with an external buffer, meaning it's a direct conversion. Signed-off-by: Kent Overstreet Cc: Dan Williams Cc: Dave Hansen Cc: nvdimm@lists.linux.dev --- tools/testing/nvdimm/test/ndtest.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/tools/testing/nvdimm/test/ndtest.c b/tools/testing/nvdimm/test= /ndtest.c index 3ca7c32e93..e9b642f7f8 100644 --- a/tools/testing/nvdimm/test/ndtest.c +++ b/tools/testing/nvdimm/test/ndtest.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include =20 #include "../watermark.h" #include "nfit_test.h" @@ -797,32 +797,30 @@ static ssize_t flags_show(struct device *dev, { struct nvdimm *nvdimm =3D to_nvdimm(dev); struct ndtest_dimm *dimm =3D nvdimm_provider_data(nvdimm); - struct seq_buf s; + struct printbuf s =3D PRINTBUF_EXTERN(buf, PAGE_SIZE); u64 flags; =20 flags =3D dimm->flags; =20 - seq_buf_init(&s, buf, PAGE_SIZE); if (flags & PAPR_PMEM_UNARMED_MASK) - seq_buf_printf(&s, "not_armed "); + pr_buf(&s, "not_armed "); =20 if (flags & PAPR_PMEM_BAD_SHUTDOWN_MASK) - seq_buf_printf(&s, "flush_fail "); + pr_buf(&s, "flush_fail "); =20 if (flags & PAPR_PMEM_BAD_RESTORE_MASK) - seq_buf_printf(&s, "restore_fail "); + pr_buf(&s, "restore_fail "); =20 if (flags & PAPR_PMEM_SAVE_MASK) - seq_buf_printf(&s, "save_fail "); + pr_buf(&s, "save_fail "); =20 if (flags & PAPR_PMEM_SMART_EVENT_MASK) - seq_buf_printf(&s, "smart_notify "); + pr_buf(&s, "smart_notify "); =20 + if (printbuf_written(&s)) + pr_buf(&s, "\n"); =20 - if (seq_buf_used(&s)) - seq_buf_printf(&s, "\n"); - - return seq_buf_used(&s); + return printbuf_written(&s); } static DEVICE_ATTR_RO(flags); =20 --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C92A9C433F5 for ; Thu, 19 May 2022 17:28:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242902AbiESR2B (ORCPT ); Thu, 19 May 2022 13:28:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242808AbiESRZ6 (ORCPT ); Thu, 19 May 2022 13:25:58 -0400 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E212DFF4F; Thu, 19 May 2022 10:25:38 -0700 (PDT) Received: by mail-pj1-x1031.google.com with SMTP id l7-20020a17090aaa8700b001dd1a5b9965so5842865pjq.2; Thu, 19 May 2022 10:25:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rEarPetEXD2YQGYpP7hMIqZiYpJpE1Z8lNf7Sa8/vnM=; b=BvKT+R1LR16GPqLiARV7wcahKZ+cTX7B1WFyzIgW4hu0pijp9ZDuxh7G7Lb+PRW9Ro 2qoy61P1sC7EX5TaLJCfxaTiqwMSUfKj+HKHWv+XESmte/fUTDq8rX+MMG2DXMvgLzb7 BAJGmPZvtx8klQ8YYw63yWIcOx50kplligC27G+nY/HwDNThlOE7pmHTd5e9ar5kGBYc nG6BWGZqdWOD9Hhqg0uIySkC7895FxVxRlASpPpSjZ/6K9hgcvQDQUXwLDKUgrjIMfpl bYtM5L6p+M6dNYAm9uwUmSg3BV7HTuZMsbIZ9WDXvW0pmP0VpOvFOQlrdvEMezW5gDsz jHPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rEarPetEXD2YQGYpP7hMIqZiYpJpE1Z8lNf7Sa8/vnM=; b=c1kKMO9W66i/u1NgkE/7xmFssrloIMZ3ysSbtIIf2a3K3hmoP/MEZL8aVDedk9QY4S 7V6EtRY+KTflI8ohnxpW7jT7rXRm/nkhhE7Gu6FsW0S92/E3VE0XLYyFXvx0WO8sNpgY U6STrlXQDi3/cV0UlcxFKibmgZCcFhUYEdnDlM8Q7xA3Tunf4BBOPsrtRfEosY6iFXwj O6C96FRls3n66dcQPWBHoiHqXoBcEIXyGBaRKLpeBWqefmZiajTtE9CHYjFd0scdLWuR bawYA1hWWeMSsoxNfm0RWwZCEg8YhSQXN2x7h7AwsrmdDtR8oFlDIeYRfL+iN/XungB0 jSnQ== X-Gm-Message-State: AOAM5309rjHi+elr2GN71UcQVRQUnNym9CSqnQ5k+XgVfYvKPU34q41c Fel5YAbkpEIwk2NFmt7sXD7J5ygKOdM/ X-Google-Smtp-Source: ABdhPJx077FrrQzngojinN9YIPS8DTnwKt1wyQ2e8JYS8VQR6eUnIjK3DYbihQXLLI0Y31FjLK4qjw== X-Received: by 2002:a17:903:1211:b0:15e:8208:8cc0 with SMTP id l17-20020a170903121100b0015e82088cc0mr5844421plh.52.1652981136600; Thu, 19 May 2022 10:25:36 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.25.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:25:35 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH v2 26/28] powerpc: Convert to printbuf Date: Thu, 19 May 2022 13:24:19 -0400 Message-Id: <20220519172421.162394-27-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This converts from seq_buf to printbuf. We're using printbuf in external buffer mode, so it's a direct conversion, aside from some trivial refactoring in cpu_show_meltdown() to make the code more consistent. Signed-off-by: Kent Overstreet Cc: linuxppc-dev@lists.ozlabs.org --- arch/powerpc/kernel/process.c | 16 +++-- arch/powerpc/kernel/security.c | 75 ++++++++++------------- arch/powerpc/platforms/pseries/papr_scm.c | 34 +++++----- 3 files changed, 57 insertions(+), 68 deletions(-) diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c index 984813a4d5..f6f7804516 100644 --- a/arch/powerpc/kernel/process.c +++ b/arch/powerpc/kernel/process.c @@ -39,7 +39,7 @@ #include #include #include -#include +#include =20 #include #include @@ -1399,32 +1399,30 @@ void show_user_instructions(struct pt_regs *regs) { unsigned long pc; int n =3D NR_INSN_TO_PRINT; - struct seq_buf s; char buf[96]; /* enough for 8 times 9 + 2 chars */ + struct printbuf s =3D PRINTBUF_EXTERN(buf, sizeof(buf)); =20 pc =3D regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int)); =20 - seq_buf_init(&s, buf, sizeof(buf)); - while (n) { int i; =20 - seq_buf_clear(&s); + printbuf_reset(&s); =20 for (i =3D 0; i < 8 && n; i++, n--, pc +=3D sizeof(int)) { int instr; =20 if (copy_from_user_nofault(&instr, (void __user *)pc, sizeof(instr))) { - seq_buf_printf(&s, "XXXXXXXX "); + pr_buf(&s, "XXXXXXXX "); continue; } - seq_buf_printf(&s, regs->nip =3D=3D pc ? "<%08x> " : "%08x ", instr); + pr_buf(&s, regs->nip =3D=3D pc ? "<%08x> " : "%08x ", instr); } =20 - if (!seq_buf_has_overflowed(&s)) + if (printbuf_remaining(&s)) pr_info("%s[%d]: code: %s\n", current->comm, - current->pid, s.buffer); + current->pid, s.buf); } } =20 diff --git a/arch/powerpc/kernel/security.c b/arch/powerpc/kernel/security.c index e159d4093d..5c9bad138c 100644 --- a/arch/powerpc/kernel/security.c +++ b/arch/powerpc/kernel/security.c @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include =20 #include @@ -144,31 +144,28 @@ void __init setup_spectre_v2(void) #ifdef CONFIG_PPC_BOOK3S_64 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *att= r, char *buf) { + struct printbuf s =3D PRINTBUF_EXTERN(buf, PAGE_SIZE); bool thread_priv; =20 thread_priv =3D security_ftr_enabled(SEC_FTR_L1D_THREAD_PRIV); =20 if (rfi_flush) { - struct seq_buf s; - seq_buf_init(&s, buf, PAGE_SIZE - 1); =20 - seq_buf_printf(&s, "Mitigation: RFI Flush"); + pr_buf(&s, "Mitigation: RFI Flush"); if (thread_priv) - seq_buf_printf(&s, ", L1D private per thread"); - - seq_buf_printf(&s, "\n"); - - return s.len; + pr_buf(&s, ", L1D private per thread"); + + pr_buf(&s, "\n"); + } else if (thread_priv) { + pr_buf(&s, "Vulnerable: L1D private per thread\n"); + } else if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) && + !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR)) { + pr_buf(&s, "Not affected\n"); + } else { + pr_buf(&s, "Vulnerable\n"); } =20 - if (thread_priv) - return sprintf(buf, "Vulnerable: L1D private per thread\n"); - - if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) && - !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR)) - return sprintf(buf, "Not affected\n"); - - return sprintf(buf, "Vulnerable\n"); + return printbuf_written(&s); } =20 ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, c= har *buf) @@ -179,70 +176,66 @@ ssize_t cpu_show_l1tf(struct device *dev, struct devi= ce_attribute *attr, char *b =20 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *a= ttr, char *buf) { - struct seq_buf s; - - seq_buf_init(&s, buf, PAGE_SIZE - 1); + struct printbuf s =3D PRINTBUF_EXTERN(buf, PAGE_SIZE); =20 if (security_ftr_enabled(SEC_FTR_BNDS_CHK_SPEC_BAR)) { if (barrier_nospec_enabled) - seq_buf_printf(&s, "Mitigation: __user pointer sanitization"); + pr_buf(&s, "Mitigation: __user pointer sanitization"); else - seq_buf_printf(&s, "Vulnerable"); + pr_buf(&s, "Vulnerable"); =20 if (security_ftr_enabled(SEC_FTR_SPEC_BAR_ORI31)) - seq_buf_printf(&s, ", ori31 speculation barrier enabled"); + pr_buf(&s, ", ori31 speculation barrier enabled"); =20 - seq_buf_printf(&s, "\n"); + pr_buf(&s, "\n"); } else - seq_buf_printf(&s, "Not affected\n"); + pr_buf(&s, "Not affected\n"); =20 - return s.len; + return printbuf_written(&s); } =20 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *a= ttr, char *buf) { - struct seq_buf s; + struct printbuf s =3D PRINTBUF_EXTERN(buf, PAGE_SIZE); bool bcs, ccd; =20 - seq_buf_init(&s, buf, PAGE_SIZE - 1); - bcs =3D security_ftr_enabled(SEC_FTR_BCCTRL_SERIALISED); ccd =3D security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED); =20 if (bcs || ccd) { - seq_buf_printf(&s, "Mitigation: "); + pr_buf(&s, "Mitigation: "); =20 if (bcs) - seq_buf_printf(&s, "Indirect branch serialisation (kernel only)"); + pr_buf(&s, "Indirect branch serialisation (kernel only)"); =20 if (bcs && ccd) - seq_buf_printf(&s, ", "); + pr_buf(&s, ", "); =20 if (ccd) - seq_buf_printf(&s, "Indirect branch cache disabled"); + pr_buf(&s, "Indirect branch cache disabled"); =20 } else if (count_cache_flush_type !=3D BRANCH_CACHE_FLUSH_NONE) { - seq_buf_printf(&s, "Mitigation: Software count cache flush"); + pr_buf(&s, "Mitigation: Software count cache flush"); =20 if (count_cache_flush_type =3D=3D BRANCH_CACHE_FLUSH_HW) - seq_buf_printf(&s, " (hardware accelerated)"); + pr_buf(&s, " (hardware accelerated)"); =20 } else if (btb_flush_enabled) { - seq_buf_printf(&s, "Mitigation: Branch predictor state flush"); + pr_buf(&s, "Mitigation: Branch predictor state flush"); } else { - seq_buf_printf(&s, "Vulnerable"); + pr_buf(&s, "Vulnerable"); } =20 if (bcs || ccd || count_cache_flush_type !=3D BRANCH_CACHE_FLUSH_NONE) { if (link_stack_flush_type !=3D BRANCH_CACHE_FLUSH_NONE) - seq_buf_printf(&s, ", Software link stack flush"); + pr_buf(&s, ", Software link stack flush"); if (link_stack_flush_type =3D=3D BRANCH_CACHE_FLUSH_HW) - seq_buf_printf(&s, " (hardware accelerated)"); + pr_buf(&s, " (hardware accelerated)"); } =20 - seq_buf_printf(&s, "\n"); + pr_buf(&s, "\n"); =20 - return s.len; + return printbuf_written(&s); } =20 #ifdef CONFIG_PPC_BOOK3S_64 diff --git a/arch/powerpc/platforms/pseries/papr_scm.c b/arch/powerpc/platf= orms/pseries/papr_scm.c index f48e87ac89..e55a9af397 100644 --- a/arch/powerpc/platforms/pseries/papr_scm.c +++ b/arch/powerpc/platforms/pseries/papr_scm.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include =20 #include @@ -843,7 +843,7 @@ static ssize_t perf_stats_show(struct device *dev, { int index; ssize_t rc; - struct seq_buf s; + struct printbuf s =3D PRINTBUF_EXTERN(buf, PAGE_SIZE); struct papr_scm_perf_stat *stat; struct papr_scm_perf_stats *stats; struct nvdimm *dimm =3D to_nvdimm(dev); @@ -866,18 +866,17 @@ static ssize_t perf_stats_show(struct device *dev, * values. Since stat_id is essentially a char string of * 8 bytes, simply use the string format specifier to print it. */ - seq_buf_init(&s, buf, PAGE_SIZE); for (index =3D 0, stat =3D stats->scm_statistic; index < be32_to_cpu(stats->num_statistics); ++index, ++stat) { - seq_buf_printf(&s, "%.8s =3D 0x%016llX\n", - stat->stat_id, - be64_to_cpu(stat->stat_val)); + pr_buf(&s, "%.8s =3D 0x%016llX\n", + stat->stat_id, + be64_to_cpu(stat->stat_val)); } =20 free_stats: kfree(stats); - return rc ? rc : (ssize_t)seq_buf_used(&s); + return rc ?: printbuf_written(&s); } static DEVICE_ATTR_ADMIN_RO(perf_stats); =20 @@ -886,7 +885,7 @@ static ssize_t flags_show(struct device *dev, { struct nvdimm *dimm =3D to_nvdimm(dev); struct papr_scm_priv *p =3D nvdimm_provider_data(dimm); - struct seq_buf s; + struct printbuf s =3D PRINTBUF_EXTERN(buf, PAGE_SIZE); u64 health; int rc; =20 @@ -897,29 +896,28 @@ static ssize_t flags_show(struct device *dev, /* Copy health_bitmap locally, check masks & update out buffer */ health =3D READ_ONCE(p->health_bitmap); =20 - seq_buf_init(&s, buf, PAGE_SIZE); if (health & PAPR_PMEM_UNARMED_MASK) - seq_buf_printf(&s, "not_armed "); + pr_buf(&s, "not_armed "); =20 if (health & PAPR_PMEM_BAD_SHUTDOWN_MASK) - seq_buf_printf(&s, "flush_fail "); + pr_buf(&s, "flush_fail "); =20 if (health & PAPR_PMEM_BAD_RESTORE_MASK) - seq_buf_printf(&s, "restore_fail "); + pr_buf(&s, "restore_fail "); =20 if (health & PAPR_PMEM_ENCRYPTED) - seq_buf_printf(&s, "encrypted "); + pr_buf(&s, "encrypted "); =20 if (health & PAPR_PMEM_SMART_EVENT_MASK) - seq_buf_printf(&s, "smart_notify "); + pr_buf(&s, "smart_notify "); =20 if (health & PAPR_PMEM_SCRUBBED_AND_LOCKED) - seq_buf_printf(&s, "scrubbed locked "); + pr_buf(&s, "scrubbed locked "); =20 - if (seq_buf_used(&s)) - seq_buf_printf(&s, "\n"); + if (printbuf_written(&s)) + pr_buf(&s, "\n"); =20 - return seq_buf_used(&s); + return printbuf_written(&s); } DEVICE_ATTR_RO(flags); =20 --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0044C433F5 for ; Thu, 19 May 2022 17:28:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242915AbiESR2F (ORCPT ); Thu, 19 May 2022 13:28:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242862AbiESR0J (ORCPT ); Thu, 19 May 2022 13:26:09 -0400 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9AF6ADFF6A; Thu, 19 May 2022 10:25:40 -0700 (PDT) Received: by mail-pl1-x631.google.com with SMTP id s14so5374712plk.8; Thu, 19 May 2022 10:25:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Conle3R7/rWLrdvm5pHUbuFYVfzx4ZhW28hlaDoo5UE=; b=h6nyI75WW6zrfYSF1n9gm4XCazCn1PGX6/HpgzdprVrmdogWSiHO3kJjCPbEgsqbax xx+VfzKLCLRocuARWkTVz1ctSLEeuDN+M5Wr4h6N4FFviA1UdQrZh1GwiwRAQDR0zLHO /fVg1CAoMI/APsK5Vbxs02R9nvaMunqQ18aOgFoq+5/cWUA951dj/hlSgVzh+y+rXMh2 7YAZfgDC5V6BIOE3O+xeys/OtT8Wv/60sS5xaGcmS2SppkVYHPvc3KTssYxFMWCSs9ak lAVxSFOwo24iNnk5A6J7e9jjn9OyAa9R9YU83Qku0+/1m8vRk8D9YnUMuZKul7frOXMM TUyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Conle3R7/rWLrdvm5pHUbuFYVfzx4ZhW28hlaDoo5UE=; b=kRBku3ynqv4m8zvu14mWu40umivqboCErFAEUQjyjG4kY25MIG03Y906+/qNNfCnx/ TvqEYL95YBgH+xomRxN84bjr4Wb4a0HUzkxC1fgZbh8sfsWHcLJtSWV3EY5chaQTYCOf fTWDpIl3ghIsc0M3lNnhgXjs2wgD0cGMRCMerALTtjQSO/llT3uVAztVH2xSVLonzaAm /X04qTycms2zRP8JYlrX/nibJo8gxGK88uzOixeKfP6HxJwY97vMfRxiQZPMjpq3grT3 r8iekYwgj8vrassZG3Hk3qW6zHCQXouaRJ22W90HDI7zzrvtk7qHrl5IjSPO0jlR0l/K VClQ== X-Gm-Message-State: AOAM531/jgmcnLdj2b4D0VVttOaJiQiPiJREWqBHDIJQ8hKxS+XUlLhy VhGMU89lLj7KDc4Tsrhac8UvkOwjIHh1 X-Google-Smtp-Source: ABdhPJwPhcmLCAs6EVuey2Jc5ggnwZaqeYmHuvaGHSvzD3g45sk0aMZm/vyLoY0KgK+9sbwRmyCGxw== X-Received: by 2002:a17:90a:e392:b0:1df:b078:8dbe with SMTP id b18-20020a17090ae39200b001dfb0788dbemr6776271pjz.105.1652981139849; Thu, 19 May 2022 10:25:39 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.25.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:25:39 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org, x86@kernel.org Subject: [PATCH v2 27/28] x86/resctrl: Convert to printbuf Date: Thu, 19 May 2022 13:24:20 -0400 Message-Id: <20220519172421.162394-28-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This converts from seq_buf to printbuf. We're using printbuf in external buffer mode, so it's a direct conversion, aside from some trivial refactoring in cpu_show_meltdown() to make the code more consistent. Signed-off-by: Kent Overstreet Cc: x86@kernel.org --- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/r= esctrl/rdtgroup.c index b57b3db9a6..8e4f7773b7 100644 --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c @@ -19,7 +19,7 @@ #include #include #include -#include +#include #include #include #include @@ -51,7 +51,7 @@ static struct kernfs_node *kn_mongrp; /* Kernel fs node for "mon_data" directory under root */ static struct kernfs_node *kn_mondata; =20 -static struct seq_buf last_cmd_status; +static struct printbuf last_cmd_status; static char last_cmd_status_buf[512]; =20 struct dentry *debugfs_resctrl; @@ -59,13 +59,13 @@ struct dentry *debugfs_resctrl; void rdt_last_cmd_clear(void) { lockdep_assert_held(&rdtgroup_mutex); - seq_buf_clear(&last_cmd_status); + printbuf_reset(&last_cmd_status); } =20 void rdt_last_cmd_puts(const char *s) { lockdep_assert_held(&rdtgroup_mutex); - seq_buf_puts(&last_cmd_status, s); + pr_str(&last_cmd_status, s); } =20 void rdt_last_cmd_printf(const char *fmt, ...) @@ -74,7 +74,7 @@ void rdt_last_cmd_printf(const char *fmt, ...) =20 va_start(ap, fmt); lockdep_assert_held(&rdtgroup_mutex); - seq_buf_vprintf(&last_cmd_status, fmt, ap); + vpr_buf(&last_cmd_status, fmt, ap); va_end(ap); } =20 @@ -833,7 +833,7 @@ static int rdt_last_cmd_status_show(struct kernfs_open_= file *of, int len; =20 mutex_lock(&rdtgroup_mutex); - len =3D seq_buf_used(&last_cmd_status); + len =3D printbuf_written(&last_cmd_status); if (len) seq_printf(seq, "%.*s", len, last_cmd_status_buf); else @@ -3248,8 +3248,8 @@ int __init rdtgroup_init(void) { int ret =3D 0; =20 - seq_buf_init(&last_cmd_status, last_cmd_status_buf, - sizeof(last_cmd_status_buf)); + last_cmd_status =3D PRINTBUF_EXTERN(last_cmd_status_buf, + sizeof(last_cmd_status_buf)); =20 ret =3D rdtgroup_setup_root(); if (ret) --=20 2.36.0 From nobody Thu May 7 19:51:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BC075C433F5 for ; Thu, 19 May 2022 17:27:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239243AbiESR1x (ORCPT ); Thu, 19 May 2022 13:27:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242823AbiESRZ6 (ORCPT ); Thu, 19 May 2022 13:25:58 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9DB6F332; Thu, 19 May 2022 10:25:42 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id oe17-20020a17090b395100b001df77d29587so9369262pjb.2; Thu, 19 May 2022 10:25:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=brt3Yeauzwa5CMMxNRUyLq/zIk6506yKuvqwwF5DMC8=; b=JSJdcqdSDHPjGSo+xJVkbFOfu6AOq9y9IhGph92HdwJ9MI/md7HGxr+0wkTaI+xCTl 4t96zC4F+ojZ6FCrrCQrG1mWOmOzNaR8Ktxz6/dolYM9VvRhqhqMRkvlPffCpt9VwykO s2msXPGgxgbtioNQwfUaqOs9xfTQjgR+MUSf+TavLIkvPQ3C4E86pKabt8uM0tPzqP9W FfOlJYzShD+Dg/Of9SseQ9vyGLfvezn4cnwWRhEow0rOASJZz0I6E0SKaWcrR8KjqaKr e3OgDZUH8v1FkJ3np7ID3xTI+Du8pLJycLmew+44eYLIPrDcarYPLfLyFrFL2hYo7+QG HoOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=brt3Yeauzwa5CMMxNRUyLq/zIk6506yKuvqwwF5DMC8=; b=gpepObByyqoC1Ev+gMSONyJ33nyq0bmA4PWTTs3ijB+tEYZYJIvjic/HbZYhvzrwrZ C8QJOpsJ81B/fAZyuHgCiUQrVkH1es9gC9mvTidGcHp/pYOwtNpkvpx70vtVA6Raa3oB VXDZqBe3C7T0mHUXS3qMnNgo+1KjVJ7kASzaRQdLSRB1GTW5U2Lvs+71+9iZpUIRPd3/ yBtHK+2MmFIebzHFB+JCYP9b8wkG0+kFBR1cueT5vERVMg1j9hjvn+DLbwG8TIMKhIan q0nGAnzODt3o/Qt5nOSf3EEtSAF0VUht9Ak0x98sR3AKyRlsWi0DgxyHqA9eb8Y8Yl61 xInw== X-Gm-Message-State: AOAM530EjPZj59lXcrBDWNN8ClRLMLeAMtqLaAXryNpggQUBzZZ5GgWW kyGgC25qXHM1WV1UkaR1fAC2Y/lTaUqR X-Google-Smtp-Source: ABdhPJyRNZ55YmuAKFNKOY/V8a+Mw2JojTXrQcsM6jyTPrXRQTybLjqXiyjSo1Fyqw9hTeU1Y0M+/w== X-Received: by 2002:a17:90b:3903:b0:1df:d0b4:3b54 with SMTP id ob3-20020a17090b390300b001dfd0b43b54mr5463906pjb.40.1652981141801; Thu, 19 May 2022 10:25:41 -0700 (PDT) Received: from zaphod.evilpiepirate.org (068-119-229-002.res.spectrum.com. [68.119.229.2]) by smtp.gmail.com with ESMTPSA id y4-20020a655a04000000b003c6ab6ba06csm3859126pgs.79.2022.05.19.10.25.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 10:25:41 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@vger.kernel.org, pmladek@suse.com, rostedt@goodmis.org, senozhatsky@chromium.org Cc: Kent Overstreet , andriy.shevchenko@linux.intel.com, willy@infradead.org, linux-pci@vger.kernel.org Subject: [PATCH v2 28/28] PCI/P2PDMA: Convert to printbuf Date: Thu, 19 May 2022 13:24:21 -0400 Message-Id: <20220519172421.162394-29-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220519172421.162394-1-kent.overstreet@gmail.com> References: <20220519172421.162394-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This converts from seq_buf to printbuf. We're using printbuf in external buffer mode, so it's a direct conversion, aside from some trivial refactoring in cpu_show_meltdown() to make the code more consistent. Signed-off-by: Kent Overstreet Cc: linux-pci@vger.kernel.org --- drivers/pci/p2pdma.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/drivers/pci/p2pdma.c b/drivers/pci/p2pdma.c index 1015274bd2..0d5c993386 100644 --- a/drivers/pci/p2pdma.c +++ b/drivers/pci/p2pdma.c @@ -17,7 +17,7 @@ #include #include #include -#include +#include #include =20 enum pci_p2pdma_map_type { @@ -281,12 +281,9 @@ static int pci_bridge_has_acs_redir(struct pci_dev *pd= ev) return 0; } =20 -static void seq_buf_print_bus_devfn(struct seq_buf *buf, struct pci_dev *p= dev) +static void pr_bus_devfn(struct printbuf *buf, struct pci_dev *pdev) { - if (!buf) - return; - - seq_buf_printf(buf, "%s;", pci_name(pdev)); + pr_buf(buf, "%s;", pci_name(pdev)); } =20 static bool cpu_supports_p2pdma(void) @@ -454,13 +451,11 @@ calc_map_type_and_dist(struct pci_dev *provider, stru= ct pci_dev *client, struct pci_dev *a =3D provider, *b =3D client, *bb; bool acs_redirects =3D false; struct pci_p2pdma *p2pdma; - struct seq_buf acs_list; int acs_cnt =3D 0; int dist_a =3D 0; int dist_b =3D 0; char buf[128]; - - seq_buf_init(&acs_list, buf, sizeof(buf)); + struct printbuf acs_list =3D PRINTBUF_EXTERN(buf, sizeof(buf)); =20 /* * Note, we don't need to take references to devices returned by @@ -471,7 +466,7 @@ calc_map_type_and_dist(struct pci_dev *provider, struct= pci_dev *client, dist_b =3D 0; =20 if (pci_bridge_has_acs_redir(a)) { - seq_buf_print_bus_devfn(&acs_list, a); + pr_bus_devfn(&acs_list, a); acs_cnt++; } =20 @@ -500,7 +495,7 @@ calc_map_type_and_dist(struct pci_dev *provider, struct= pci_dev *client, break; =20 if (pci_bridge_has_acs_redir(bb)) { - seq_buf_print_bus_devfn(&acs_list, bb); + pr_bus_devfn(&acs_list, bb); acs_cnt++; } =20 --=20 2.36.0