From patchwork Mon Jun 20 00:42:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Overstreet X-Patchwork-Id: 12886903 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7B2B4C43334 for ; Mon, 20 Jun 2022 00:43:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 169036B0075; Sun, 19 Jun 2022 20:43:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 119796B0083; Sun, 19 Jun 2022 20:43:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F25126B0085; Sun, 19 Jun 2022 20:43:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E3FE06B0075 for ; Sun, 19 Jun 2022 20:43:01 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B9B29343E0 for ; Mon, 20 Jun 2022 00:43:01 +0000 (UTC) X-FDA: 79596764562.17.4478E8D Received: from mail-qk1-f170.google.com (mail-qk1-f170.google.com [209.85.222.170]) by imf09.hostedemail.com (Postfix) with ESMTP id D8E9E140019 for ; Mon, 20 Jun 2022 00:43:00 +0000 (UTC) Received: by mail-qk1-f170.google.com with SMTP id g15so6877681qke.4 for ; Sun, 19 Jun 2022 17:43:00 -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=87vr5hheCjxa+C+zJDx+UhCXUzg+Bvxner7x63Be5z0=; b=ocbtVxqlFRKBq2hJSLkb1jSAZI+z+3QG9/JSVg+4RFJ1C/ow58U8dZhOK82LCCUPOB 3HRqKTj9Vp+ypkb2xtG+NnDyZobOOfEob5du36/KgfSpRRen4GXLuTwz7CXEUpsCaCrA +L5PUA1xlnrN0JSOja7JY9f+NodUqwMC92vxSt0iM9vHyNyEzklb13A3yLvzlmrcjC/k yFACZSug+zUrJkcC+DiHt70wi+qd6LAArh2Zp4szCWvuqY92HgZqwFeQjeT5rcl99DMp ml+k7YYcbgaj5BXOB3em38kBqjfsUY3OzwQ5+dsgnScr1TBqSN+Un3SRuBO3CY3ws5wt k3JQ== 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=87vr5hheCjxa+C+zJDx+UhCXUzg+Bvxner7x63Be5z0=; b=cUx9y8OLJQe1hf+UT/hPYBIa36MeamRfXUngKcNr1bi5ZjBZaZCJj2u+vKFyQAb7Rd BUG2xghY/gQkM1kuYFdD0vvkNA5kQGAGWwHvWHJyPr6aHPNra5sjW+4biZUY687YOxlE YRU+8gtKjYppOQCdEzsODgofcZFxdUUqfb/lgdlUI6p7JE/zHKXNpH8dOwF/IFj1UH3T Rhmb+LQ3IIqagKaQfZU60+1wcz5tLI+MWVwj1FAJcMfA7FZTyBEGh2gveNGiJYGwHgAW EmrBQqiFvtzReKcqQ4iH5XP+YXQ3KfsVtJWldkRCZ88f9vy334jrUvAhu0uRc2dmLSYw DBaQ== X-Gm-Message-State: AJIora8pBZQilHTTYRdmhXfDt4htW8R3G6GbZ5ny2GXPLdcs18VSlylF 1xA/QbCDlUtqXpLukijAylt74CY99Hi7qL4= X-Google-Smtp-Source: AGRyM1vHltOnT6lZQxwYcHcDmIe/iCQZuLx8M068VFsShpd1IhADyohd3Ftmhz+guiDL0E8AM8B5gQ== X-Received: by 2002:a37:ad09:0:b0:6a6:ae2b:9d54 with SMTP id f9-20020a37ad09000000b006a6ae2b9d54mr14777059qkm.424.1655685780208; Sun, 19 Jun 2022 17:43:00 -0700 (PDT) Received: from localhost (c-73-219-103-14.hsd1.vt.comcast.net. [73.219.103.14]) by smtp.gmail.com with ESMTPSA id y26-20020ac87c9a000000b00304ecf35b50sm9055654qtv.97.2022.06.19.17.42.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Jun 2022 17:42:59 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, pmladek@suse.com Cc: Kent Overstreet , rostedt@goodmis.org, enozhatsky@chromium.org, linux@rasmusvillemoes.dk, willy@infradead.org Subject: [PATCH v4 12/34] vsprintf: prt_u64_minwidth(), prt_u64() Date: Sun, 19 Jun 2022 20:42:11 -0400 Message-Id: <20220620004233.3805-13-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220620004233.3805-1-kent.overstreet@gmail.com> References: <20220620004233.3805-1-kent.overstreet@gmail.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655685780; a=rsa-sha256; cv=none; b=2JQNoWrMhi9P9jqmRwai0DdiRpue56cVMj9A9W3C+mSlQ6YI6Npbm5wWrPtfVCeUgDsC/C HIf8VTU6t+gvyNH2urZ5AbFi2PUKeJpaaSWKHiKH2K8gsNUHEziPvdlHV/Zujlpf91/gVf VA0VdPQIZwV6sOORYrVStd3dcjZqHrk= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=ocbtVxql; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf09.hostedemail.com: domain of kent.overstreet@gmail.com designates 209.85.222.170 as permitted sender) smtp.mailfrom=kent.overstreet@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655685780; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=87vr5hheCjxa+C+zJDx+UhCXUzg+Bvxner7x63Be5z0=; b=ReIiPrEHTiR+piAC5Q4KH/1Rv05vHyDzZblgpqu2rklA1+TQP2Yfjm/FvBdT01rkt0N+2W pygAHzP56ciuuVQdOa18zoDOHw2Mfj700KX2qsuxAmmYBfZY4ky0xTyBo8kOP7Zp0lvcXC UXF/KNVMhNtwZTx58nNKG4Y1pJ+b8z4= X-Rspamd-Queue-Id: D8E9E140019 X-Rspam-User: Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=ocbtVxql; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf09.hostedemail.com: domain of kent.overstreet@gmail.com designates 209.85.222.170 as permitted sender) smtp.mailfrom=kent.overstreet@gmail.com X-Rspamd-Server: rspam06 X-Stat-Signature: unm38q9pcg94reujnomz1ffscapizgm9 X-HE-Tag: 1655685780-787677 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- 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 1906861ece..9ba5a53c6a 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -202,12 +202,14 @@ static inline void might_fault(void) { } void do_exit(long error_code) __noreturn; +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); /* lib/printf utilities */ -struct printbuf; extern __printf(2, 3) void prt_printf(struct printbuf *out, const char *fmt, ...); extern __printf(2, 0) void prt_vprintf(struct printbuf *out, const char *fmt, va_list); diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 7d20406deb..e65115f90f 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -368,41 +368,51 @@ char *put_dec(char *buf, unsigned long long n) #endif -/* - * 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 width) +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 = put_dec(tmp, num) - tmp; - /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ - if (num <= 9) { - tmp[0] = '0' + num; - len = 1; - } else { - len = put_dec(tmp, num) - tmp; - } + printbuf_make_room(out, max(len, width)); - if (len > size || width > size) - return 0; + if (width > len) + __prt_chars_reserved(out, '0', width - len); - if (width > len) { - width = width - len; - for (idx = 0; idx < width; idx++) - buf[idx] = ' '; - } else { - width = 0; - } + while (len) + __prt_char_reserved(out, tmp[--len]); + printbuf_nul_terminate(out); +} - for (idx = 0; idx < len; ++idx) - buf[idx + width] = 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); +} - 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 width) +{ + struct printbuf out = PRINTBUF_EXTERN(buf, size); + + prt_u64_minwidth(&out, num, width); + return out.pos; } #define SIGN 1 /* unsigned/signed, must be 1 */ @@ -1018,20 +1028,6 @@ static const struct printf_spec default_dec_spec = { .precision = -1, }; -static const struct printf_spec default_dec02_spec = { - .base = 10, - .field_width = 2, - .precision = -1, - .flags = ZEROPAD, -}; - -static const struct printf_spec default_dec04_spec = { - .base = 10, - .field_width = 4, - .precision = -1, - .flags = ZEROPAD, -}; - static noinline_for_stack void resource_string(struct printbuf *out, struct resource *res, struct printf_spec spec, const char *fmt) @@ -1231,12 +1227,12 @@ void bitmap_list_string(struct printbuf *out, unsigned long *bitmap, prt_char(out, ','); first = false; - number(out, rbot, default_dec_spec); + prt_u64(out, rbot); if (rtop == rbot + 1) continue; prt_char(out, '-'); - number(out, rtop - 1, default_dec_spec); + prt_u64(out, rtop - 1); } } @@ -1778,21 +1774,21 @@ void date_str(struct printbuf *out, int year = tm->tm_year + (r ? 0 : 1900); int mon = tm->tm_mon + (r ? 0 : 1); - 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); } 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); } static noinline_for_stack @@ -2070,7 +2066,7 @@ void device_node_string(struct printbuf *out, struct device_node *dn, str_spec.precision = 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 = fwnode_get_name(of_fwnode_handle(dn));