From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>,
Daniel Borkmann <daniel@iogearbox.net>,
Florent Revest <revest@google.com>,
Steven Rostedt <rostedt@goodmis.org>,
LKML <linux-kernel@vger.kernel.org>, bpf <bpf@vger.kernel.org>
Subject: Re: [PATCH] vsprintf: simplify number handling
Date: Wed, 18 Dec 2024 10:19:03 +0100 [thread overview]
Message-ID: <871py5gxh4.fsf@prevas.dk> (raw)
In-Reply-To: <20241218013620.1679088-1-torvalds@linux-foundation.org> (Linus Torvalds's message of "Tue, 17 Dec 2024 17:32:09 -0800")
On Tue, Dec 17 2024, Linus Torvalds <torvalds@linux-foundation.org> wrote:
> Instead of dealing with all the different special types (size_t,
> unsigned char, ptrdiff_t..) just deal with the size of the integer type
> and the sign.
>
> This avoids a lot of unnecessary case statements, and the games we play
> with the value of the 'SIGN' flags value
>
> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
> ---
>
> NOTE! Only very lightly tested. Also meant to be purely preparatory.
> It might be broken.
>
> I started doing this in the hope that the vsnprintf() core code could
> maybe be further cleaned up enough that it would actually be something
> we could use more generally and export to other users that want to
> basically do the printk format handling.
>
> The code is *not* there yet, though. Small steps.
>
> +/* Turn a 1/2/4-byte value into a 64-bit one with sign handling */
> +static unsigned long long get_num(unsigned int val, struct printf_spec spec)
> +{
> + unsigned int shift = 32 - spec.type*8;
> +
> + val <<= shift;
> + if (!(spec.flags & SIGN))
> + return val >> shift;
> + return (int)val >> shift;
> +}
> +
I think this needs to be a little more explicit that it's not just about
handling sign extension, but also truncation to the desired
width. Otherwise somebody will wonder why this isn't
if (!(spec.flags & SIGN))
return val;
return (int)(val << shift) >> shift;
[with the latter line duplicating our sign_extend32 helper].
The rest looks good.
Rasmus
next prev parent reply other threads:[~2024-12-18 9:19 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-17 17:32 [PATCH 0/3] ring-buffer: Hardening of the persistent buffer Steven Rostedt
2024-12-17 17:32 ` [PATCH 1/3] ring-buffer: Add uname to match criteria for persistent ring buffer Steven Rostedt
2024-12-17 17:46 ` Linus Torvalds
2024-12-17 18:04 ` Steven Rostedt
2024-12-17 18:19 ` Linus Torvalds
2024-12-17 18:24 ` Linus Torvalds
2024-12-17 18:42 ` Steven Rostedt
2024-12-17 19:22 ` Mathieu Desnoyers
2024-12-17 18:33 ` Steven Rostedt
2024-12-17 18:42 ` Linus Torvalds
2024-12-17 19:01 ` Steven Rostedt
2024-12-17 19:38 ` Linus Torvalds
2024-12-17 19:44 ` Steven Rostedt
2024-12-17 22:24 ` Linus Torvalds
2024-12-17 22:53 ` Steven Rostedt
2024-12-17 23:32 ` Linus Torvalds
2024-12-18 0:02 ` Linus Torvalds
2024-12-18 0:48 ` Steven Rostedt
2024-12-18 0:47 ` Alexei Starovoitov
2024-12-18 1:26 ` Linus Torvalds
2024-12-18 1:32 ` [PATCH] vsprintf: simplify number handling Linus Torvalds
2024-12-18 9:19 ` Rasmus Villemoes [this message]
2024-12-18 15:32 ` Steven Rostedt
2024-12-18 17:32 ` Linus Torvalds
2024-12-18 18:04 ` Steven Rostedt
2024-12-18 18:31 ` Steven Rostedt
2024-12-23 20:11 ` Linus Torvalds
2024-12-18 1:39 ` [PATCH 1/3] ring-buffer: Add uname to match criteria for persistent ring buffer Linus Torvalds
2024-12-18 1:53 ` Alexei Starovoitov
2024-12-17 19:03 ` Linus Torvalds
2024-12-17 19:07 ` Steven Rostedt
2024-12-17 19:14 ` Steven Rostedt
2024-12-17 18:17 ` Steven Rostedt
2024-12-17 17:32 ` [PATCH 2/3] trace/ring-buffer: Do not create module or dynamic events in boot mapped buffers Steven Rostedt
2024-12-17 17:32 ` [PATCH 3/3] trace/ring-buffer: Do not use TP_printk() formatting for " Steven Rostedt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=871py5gxh4.fsf@prevas.dk \
--to=linux@rasmusvillemoes.dk \
--cc=alexei.starovoitov@gmail.com \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=linux-kernel@vger.kernel.org \
--cc=revest@google.com \
--cc=rostedt@goodmis.org \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.