From nobody Sat Apr 11 20:56:30 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 17516C19F2A for ; Mon, 8 Aug 2022 02:41:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242814AbiHHClu (ORCPT ); Sun, 7 Aug 2022 22:41:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235146AbiHHClj (ORCPT ); Sun, 7 Aug 2022 22:41:39 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B59A21AF for ; Sun, 7 Aug 2022 19:41:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=bSGKE8iFlW5a7FQH8CgH+oMcAkiK5MKZFOT72qzciO0=; b=jt7X1iCUv5mwyJBs5nnKwXo1St DiGJj5DR1zBUtb5zHJVBOI1lreCGl/PenFWfN0U8Tmtnyq4iKrGCCkdItC4TRjJyEoQn9oGQ1Armu bnHlrUrvxN8atfe6yk3aLgo9MYq/B5OwFem8ji4pW4251g9lKeghTnkJaV5xW7a0UikdcWPLvj/JP qdRJqCPaUQyrJSWoP1/aF+UWG2mnkqAqQIMxU4H/gcvL6AeqR67+fGYDfsMQt+4NAcv8NwX3BJenO 9KOf4riy0B9lBNxU6wMPAqGt9DatOecGrwlWu+KyVADD4M2cf3v8vduyuNSB9O2EHub8JD4DHEVjQ 6M+kdVIQ==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1oKshx-00DVRj-E3; Mon, 08 Aug 2022 02:41:33 +0000 From: "Matthew Wilcox (Oracle)" To: linux-kernel@vger.kernel.org, pmladek@suse.com, Kent Overstreet Cc: Rasmus Villemoes Subject: [PATCH v5 10/32] vsprintf: prt_u64_minwidth(), prt_u64() Date: Mon, 8 Aug 2022 03:41:06 +0100 Message-Id: <20220808024128.3219082-11-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220808024128.3219082-1-willy@infradead.org> References: <20220808024128.3219082-1-willy@infradead.org> 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" From: Kent Overstreet This adds two new-style printbuf helpers for printing simple u64s, and converts num_to_str() to be a simple wrapper around prt_u64_minwidth(). Signed-off-by: Kent Overstreet Cc: Petr Mladek Cc: Rasmus Villemoes --- include/linux/kernel.h | 4 +- lib/vsprintf.c | 94 ++++++++++++++++++++---------------------- 2 files changed, 48 insertions(+), 50 deletions(-) diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 1906861ece7a..9ba5a53c6ad5 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -202,12 +202,14 @@ static inline void might_fault(void) { } =20 void do_exit(long error_code) __noreturn; =20 +struct printbuf; +extern void prt_u64_minwidth(struct printbuf *out, u64 num, unsigned width= ); +extern void prt_u64(struct printbuf *out, u64 num); extern int num_to_str(char *buf, int size, unsigned long long num, unsigned int width); =20 /* lib/printf utilities */ =20 -struct printbuf; extern __printf(2, 3) void prt_printf(struct printbuf *out, const char *fm= t, ...); extern __printf(2, 0) void prt_vprintf(struct printbuf *out, const char *f= mt, va_list); =20 diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 87adc528c6c7..6001710352c4 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -368,41 +368,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. +/** + * prt_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 prt_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) + __prt_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) + __prt_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]; +/** + * prt_u64 - print a simple u64, in decimal + * @out: printbuf to output to + * @num: u64 to print + */ +void prt_u64(struct printbuf *out, u64 num) +{ + prt_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 prt_u64() or prt_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); + + prt_u64_minwidth(&out, num, width); + return out.pos; } =20 #define SIGN 1 /* unsigned/signed, must be 1 */ @@ -993,20 +1003,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) @@ -1206,12 +1202,12 @@ void bitmap_list_string(struct printbuf *out, unsig= ned long *bitmap, prt_char(out, ','); first =3D false; =20 - number(out, rbot, default_dec_spec); + prt_u64(out, rbot); if (rtop =3D=3D rbot + 1) continue; =20 prt_char(out, '-'); - number(out, rtop - 1, default_dec_spec); + prt_u64(out, rtop - 1); } } =20 @@ -1753,21 +1749,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); + prt_u64_minwidth(out, year, 4); prt_char(out, '-'); - number(out, mon, default_dec02_spec); + prt_u64_minwidth(out, mon, 2); prt_char(out, '-'); - number(out, tm->tm_mday, default_dec02_spec); + prt_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); + prt_u64_minwidth(out, tm->tm_hour, 2); prt_char(out, ':'); - number(out, tm->tm_min, default_dec02_spec); + prt_u64_minwidth(out, tm->tm_min, 2); prt_char(out, ':'); - number(out, tm->tm_sec, default_dec02_spec); + prt_u64_minwidth(out, tm->tm_sec, 2); } =20 static noinline_for_stack @@ -2045,7 +2041,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); + prt_u64(out, (unsigned int)dn->phandle); break; case 'P': /* path-spec */ p =3D fwnode_get_name(of_fwnode_handle(dn)); --=20 2.35.1