[PATCH 10/12] unwind: Simplify unwind_user_next_fp() alignment check

Peter Zijlstra posted 12 patches 4 months, 2 weeks ago
[PATCH 10/12] unwind: Simplify unwind_user_next_fp() alignment check
Posted by Peter Zijlstra 4 months, 2 weeks ago

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 kernel/unwind/user.c |    4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

--- a/kernel/unwind/user.c
+++ b/kernel/unwind/user.c
@@ -19,7 +19,6 @@ static int unwind_user_next_fp(struct un
 {
 	const struct unwind_user_frame *frame = &fp_frame;
 	unsigned long cfa, fp, ra;
-	unsigned int shift;
 
 	if (frame->use_fp) {
 		if (state->fp < state->sp)
@@ -37,8 +36,7 @@ static int unwind_user_next_fp(struct un
 		return -EINVAL;
 
 	/* Make sure that the address is word aligned */
-	shift = sizeof(long) == 4 ? 2 : 3;
-	if (cfa & ((1 << shift) - 1))
+	if (cfa & (sizeof(long) - 1))
 		return -EINVAL;
 
 	/* Find the Return Address (RA) */
Re: [PATCH 10/12] unwind: Simplify unwind_user_next_fp() alignment check
Posted by Steven Rostedt 4 months, 1 week ago
On Wed, 24 Sep 2025 09:59:58 +0200
Peter Zijlstra <peterz@infradead.org> wrote:

I would add a change log. Something like:

  sizeof(long) is 4 or 8. Where 4 = 1 << 2 and 8 = 1 << 3.
  Calculating shift to be 2 or 3 and then passing that variable into
  (1 << shift) is the same as just using sizeof(long).

I blame lack of sleep for writing that code :-p

> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>

Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org>

-- Steve

> ---
>  kernel/unwind/user.c |    4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
> 
> --- a/kernel/unwind/user.c
> +++ b/kernel/unwind/user.c
> @@ -19,7 +19,6 @@ static int unwind_user_next_fp(struct un
>  {
>  	const struct unwind_user_frame *frame = &fp_frame;
>  	unsigned long cfa, fp, ra;
> -	unsigned int shift;
>  
>  	if (frame->use_fp) {
>  		if (state->fp < state->sp)
> @@ -37,8 +36,7 @@ static int unwind_user_next_fp(struct un
>  		return -EINVAL;
>  
>  	/* Make sure that the address is word aligned */
> -	shift = sizeof(long) == 4 ? 2 : 3;
> -	if (cfa & ((1 << shift) - 1))
> +	if (cfa & (sizeof(long) - 1))
>  		return -EINVAL;
>  
>  	/* Find the Return Address (RA) */
>
Re: [PATCH 10/12] unwind: Simplify unwind_user_next_fp() alignment check
Posted by Peter Zijlstra 3 months, 2 weeks ago
On Wed, Oct 01, 2025 at 11:55:24AM -0400, Steven Rostedt wrote:
> On Wed, 24 Sep 2025 09:59:58 +0200
> Peter Zijlstra <peterz@infradead.org> wrote:
> 
> I would add a change log. Something like:
> 
>   sizeof(long) is 4 or 8. Where 4 = 1 << 2 and 8 = 1 << 3.
>   Calculating shift to be 2 or 3 and then passing that variable into
>   (1 << shift) is the same as just using sizeof(long).

I've made it: "2^log_2(n) == n". I find it very hard to spend that many
words on something this trivial :-)
Re: [PATCH 10/12] unwind: Simplify unwind_user_next_fp() alignment check
Posted by Steven Rostedt 3 months, 2 weeks ago
On Mon, 20 Oct 2025 12:28:14 +0200
Peter Zijlstra <peterz@infradead.org> wrote:

> On Wed, Oct 01, 2025 at 11:55:24AM -0400, Steven Rostedt wrote:
> > On Wed, 24 Sep 2025 09:59:58 +0200
> > Peter Zijlstra <peterz@infradead.org> wrote:
> > 
> > I would add a change log. Something like:
> > 
> >   sizeof(long) is 4 or 8. Where 4 = 1 << 2 and 8 = 1 << 3.
> >   Calculating shift to be 2 or 3 and then passing that variable into
> >   (1 << shift) is the same as just using sizeof(long).  
> 
> I've made it: "2^log_2(n) == n". I find it very hard to spend that many
> words on something this trivial :-)

What you don't like Common Core Math[1]?

-- Steve

[1] https://3010tangents.wordpress.com/2015/03/03/common-core/
Re: [PATCH 10/12] unwind: Simplify unwind_user_next_fp() alignment check
Posted by Peter Zijlstra 3 months, 2 weeks ago
On Wed, Oct 22, 2025 at 11:20:32AM -0400, Steven Rostedt wrote:
> On Mon, 20 Oct 2025 12:28:14 +0200
> Peter Zijlstra <peterz@infradead.org> wrote:
> 
> > On Wed, Oct 01, 2025 at 11:55:24AM -0400, Steven Rostedt wrote:
> > > On Wed, 24 Sep 2025 09:59:58 +0200
> > > Peter Zijlstra <peterz@infradead.org> wrote:
> > > 
> > > I would add a change log. Something like:
> > > 
> > >   sizeof(long) is 4 or 8. Where 4 = 1 << 2 and 8 = 1 << 3.
> > >   Calculating shift to be 2 or 3 and then passing that variable into
> > >   (1 << shift) is the same as just using sizeof(long).  
> > 
> > I've made it: "2^log_2(n) == n". I find it very hard to spend that many
> > words on something this trivial :-)
> 
> What you don't like Common Core Math[1]?

I'm not sure I'm qualified to comment on that particular topic. Yes,
there are many equivalent ways of doing 'math' and it is fun to explore
them all.

Specifically: 3000-1 and 2999+1 should both be mastered and both involve
the (decimal) carry bit. If your curriculum does not provide, or treats
one as more difficult from the other, something is wrong.

The thing I've noticed with my own kids though, is that teachers often
don't master these basics themselves, and find it very hard to properly
explain these things. This then makes me wonder HTF they ever got to be
a teacher, in face of their obvious incompetence, but that's another
problem.

[ I am now having to explain and derive the abc formula for finding the
  roots of the 2nd grade polynomial to my kid because curriculum mostly
  'forgot' about proofs and provides 'tricks' instead of understanding.

  Also, there is a very nice tie-in with this derivation of the abc
  formula to the starting point of calculus, notably the expression for
  finding the extreme is of course f'(x)=0, but is found here through
  simple algebra. ]

Anyway, I find throwing heaps of natural language at a trivial statement
makes it more opaque, rather than clearer.
[tip: perf/core] unwind: Simplify unwind_user_next_fp() alignment check
Posted by tip-bot2 for Peter Zijlstra 3 months, 1 week ago
The following commit has been merged into the perf/core branch of tip:

Commit-ID:     5578534e4b92350995a20068f2e6ea3186c62d7f
Gitweb:        https://git.kernel.org/tip/5578534e4b92350995a20068f2e6ea3186c62d7f
Author:        Peter Zijlstra <peterz@infradead.org>
AuthorDate:    Tue, 23 Sep 2025 13:04:09 +02:00
Committer:     Peter Zijlstra <peterz@infradead.org>
CommitterDate: Wed, 29 Oct 2025 10:29:57 +01:00

unwind: Simplify unwind_user_next_fp() alignment check

  2^log_2(n) == n

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Link: https://patch.msgid.link/20250924080119.497867836@infradead.org
---
 kernel/unwind/user.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/kernel/unwind/user.c b/kernel/unwind/user.c
index 97a8415..9dcde79 100644
--- a/kernel/unwind/user.c
+++ b/kernel/unwind/user.c
@@ -19,7 +19,6 @@ static int unwind_user_next_fp(struct unwind_user_state *state)
 {
 	const struct unwind_user_frame *frame = &fp_frame;
 	unsigned long cfa, fp, ra;
-	unsigned int shift;
 
 	if (frame->use_fp) {
 		if (state->fp < state->sp)
@@ -37,8 +36,7 @@ static int unwind_user_next_fp(struct unwind_user_state *state)
 		return -EINVAL;
 
 	/* Make sure that the address is word aligned */
-	shift = sizeof(long) == 4 ? 2 : 3;
-	if (cfa & ((1 << shift) - 1))
+	if (cfa & (sizeof(long) - 1))
 		return -EINVAL;
 
 	/* Find the Return Address (RA) */