All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] init: Align init_task to avoid conflict with MUTEX_FLAGS
@ 2020-06-25 20:44 Stafford Horne
  2020-06-26 11:12 ` Peter Zijlstra
  0 siblings, 1 reply; 4+ messages in thread
From: Stafford Horne @ 2020-06-25 20:44 UTC (permalink / raw)
  To: LKML
  Cc: Stafford Horne, Peter Zijlstra, Paul E. McKenney, Marco Elver,
	Eric W. Biederman, Ingo Molnar, Thomas Gleixner, Mike Rapoport,
	Sami Tolvanen

When booting on 32-bit machines (seen on OpenRISC) I saw this warning
with CONFIG_DEBUG_MUTEXES turned on.

    ------------[ cut here ]------------
    WARNING: CPU: 0 PID: 0 at kernel/locking/mutex.c:1242 __mutex_unlock_slowpath+0x328/0x3ec
    DEBUG_LOCKS_WARN_ON(__owner_task(owner) != current)
    Modules linked in:
    CPU: 0 PID: 0 Comm: swapper Not tainted 5.8.0-rc1-simple-smp-00005-g2864e2171db4-dirty #179
    Call trace:
    [<(ptrval)>] dump_stack+0x34/0x48
    [<(ptrval)>] __warn+0x104/0x158
    [<(ptrval)>] ? __mutex_unlock_slowpath+0x328/0x3ec
    [<(ptrval)>] warn_slowpath_fmt+0x7c/0x94
    [<(ptrval)>] __mutex_unlock_slowpath+0x328/0x3ec
    [<(ptrval)>] mutex_unlock+0x18/0x28
    [<(ptrval)>] __cpuhp_setup_state_cpuslocked.part.0+0x29c/0x2f4
    [<(ptrval)>] ? page_alloc_cpu_dead+0x0/0x30
    [<(ptrval)>] ? start_kernel+0x0/0x684
    [<(ptrval)>] __cpuhp_setup_state+0x4c/0x5c
    [<(ptrval)>] page_alloc_init+0x34/0x68
    [<(ptrval)>] ? start_kernel+0x1a0/0x684
    [<(ptrval)>] ? early_init_dt_scan_nodes+0x60/0x70
    irq event stamp: 0

I traced this to kernel/locking/mutex.c storing 3 bits of MUTEX_FLAGS in
the task_struct pointer (mutex.owner).  There is a comment saying that
task_structs are always aligned to L1_CACHE_BYTES.  This is not true for
the init_task.

On 64-bit machines this is not a problem because symbol addresses are
naturally aligned to 64-bits providing 3 bits for MUTEX_FLAGS.  Howerver,
for 32-bit machines the symbol address only has 2 bits available.

Fix this by setting init_task alignment to at least L1_CACHE_BYTES.

Cc: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Stafford Horne <shorne@gmail.com>
---
 init/init_task.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/init/init_task.c b/init/init_task.c
index 15089d15010a..d2d2af018d0d 100644
--- a/init/init_task.c
+++ b/init/init_task.c
@@ -64,6 +64,8 @@ unsigned long init_shadow_call_stack[SCS_SIZE / sizeof(long)]
 struct task_struct init_task
 #ifdef CONFIG_ARCH_TASK_STRUCT_ON_STACK
 	__init_task_data
+#else
+	__aligned(L1_CACHE_BYTES)
 #endif
 = {
 #ifdef CONFIG_THREAD_INFO_IN_TASK
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] init: Align init_task to avoid conflict with MUTEX_FLAGS
  2020-06-25 20:44 [PATCH] init: Align init_task to avoid conflict with MUTEX_FLAGS Stafford Horne
@ 2020-06-26 11:12 ` Peter Zijlstra
  2020-06-26 11:45   ` Stafford Horne
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Zijlstra @ 2020-06-26 11:12 UTC (permalink / raw)
  To: Stafford Horne
  Cc: LKML, Paul E. McKenney, Marco Elver, Eric W. Biederman,
	Ingo Molnar, Thomas Gleixner, Mike Rapoport, Sami Tolvanen

On Fri, Jun 26, 2020 at 05:44:09AM +0900, Stafford Horne wrote:
> When booting on 32-bit machines (seen on OpenRISC) I saw this warning
> with CONFIG_DEBUG_MUTEXES turned on.

> I traced this to kernel/locking/mutex.c storing 3 bits of MUTEX_FLAGS in
> the task_struct pointer (mutex.owner).  There is a comment saying that
> task_structs are always aligned to L1_CACHE_BYTES.  This is not true for
> the init_task.
> 
> On 64-bit machines this is not a problem because symbol addresses are
> naturally aligned to 64-bits providing 3 bits for MUTEX_FLAGS.  Howerver,
> for 32-bit machines the symbol address only has 2 bits available.
> 
> Fix this by setting init_task alignment to at least L1_CACHE_BYTES.

Whoopsie, sorry about that.

> Cc: Peter Zijlstra <peterz@infradead.org>
> Signed-off-by: Stafford Horne <shorne@gmail.com>
> ---
>  init/init_task.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/init/init_task.c b/init/init_task.c
> index 15089d15010a..d2d2af018d0d 100644
> --- a/init/init_task.c
> +++ b/init/init_task.c
> @@ -64,6 +64,8 @@ unsigned long init_shadow_call_stack[SCS_SIZE / sizeof(long)]
>  struct task_struct init_task
>  #ifdef CONFIG_ARCH_TASK_STRUCT_ON_STACK
>  	__init_task_data
> +#else
> +	__aligned(L1_CACHE_BYTES)
>  #endif

Why make this conditional? task_struct_cachep (in kernel/fork.c) has
max_t(int, L1_CACHE_BYTES, ARCH_MIN_TASKALIGN) alignment, so this really
should be aligned on L1_CACHE_BYTES at least.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] init: Align init_task to avoid conflict with MUTEX_FLAGS
  2020-06-26 11:12 ` Peter Zijlstra
@ 2020-06-26 11:45   ` Stafford Horne
  2020-06-26 13:11     ` Peter Zijlstra
  0 siblings, 1 reply; 4+ messages in thread
From: Stafford Horne @ 2020-06-26 11:45 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: LKML, Paul E. McKenney, Marco Elver, Eric W. Biederman,
	Ingo Molnar, Thomas Gleixner, Mike Rapoport, Sami Tolvanen

On Fri, Jun 26, 2020 at 01:12:08PM +0200, Peter Zijlstra wrote:
> On Fri, Jun 26, 2020 at 05:44:09AM +0900, Stafford Horne wrote:
> > When booting on 32-bit machines (seen on OpenRISC) I saw this warning
> > with CONFIG_DEBUG_MUTEXES turned on.
> 
> > I traced this to kernel/locking/mutex.c storing 3 bits of MUTEX_FLAGS in
> > the task_struct pointer (mutex.owner).  There is a comment saying that
> > task_structs are always aligned to L1_CACHE_BYTES.  This is not true for
> > the init_task.
> > 
> > On 64-bit machines this is not a problem because symbol addresses are
> > naturally aligned to 64-bits providing 3 bits for MUTEX_FLAGS.  Howerver,
> > for 32-bit machines the symbol address only has 2 bits available.
> > 
> > Fix this by setting init_task alignment to at least L1_CACHE_BYTES.
> 
> Whoopsie, sorry about that.
> 
> > Cc: Peter Zijlstra <peterz@infradead.org>
> > Signed-off-by: Stafford Horne <shorne@gmail.com>
> > ---
> >  init/init_task.c | 2 ++
> >  1 file changed, 2 insertions(+)
> > 
> > diff --git a/init/init_task.c b/init/init_task.c
> > index 15089d15010a..d2d2af018d0d 100644
> > --- a/init/init_task.c
> > +++ b/init/init_task.c
> > @@ -64,6 +64,8 @@ unsigned long init_shadow_call_stack[SCS_SIZE / sizeof(long)]
> >  struct task_struct init_task
> >  #ifdef CONFIG_ARCH_TASK_STRUCT_ON_STACK
> >  	__init_task_data
> > +#else
> > +	__aligned(L1_CACHE_BYTES)
> >  #endif
> 
> Why make this conditional? task_struct_cachep (in kernel/fork.c) has
> max_t(int, L1_CACHE_BYTES, ARCH_MIN_TASKALIGN) alignment, so this really
> should be aligned on L1_CACHE_BYTES at least.

I think we can make this unconditional.  The only reason I used the condition is
because the only architecture that sets CONFIG_ARCH_TASK_STRUCT_ON_STACK is ia64
which already has 64-bit alignment.

I can change it to (not incorporating max_t(ARCH_MIN_TASKALIGN) as not sure how):

diff --git a/init/init_task.c b/init/init_task.c
index 15089d15010a..ab6173f8e6a8 100644
--- a/init/init_task.c
+++ b/init/init_task.c
@@ -65,6 +65,7 @@ struct task_struct init_task
 #ifdef CONFIG_ARCH_TASK_STRUCT_ON_STACK
        __init_task_data
 #endif
+       __aligned(L1_CACHE_BYTES)
 = {
 #ifdef CONFIG_THREAD_INFO_IN_TASK
        .thread_info    = INIT_THREAD_INFO(init_task),

-Stafford

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] init: Align init_task to avoid conflict with MUTEX_FLAGS
  2020-06-26 11:45   ` Stafford Horne
@ 2020-06-26 13:11     ` Peter Zijlstra
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Zijlstra @ 2020-06-26 13:11 UTC (permalink / raw)
  To: Stafford Horne
  Cc: LKML, Paul E. McKenney, Marco Elver, Eric W. Biederman,
	Ingo Molnar, Thomas Gleixner, Mike Rapoport, Sami Tolvanen

On Fri, Jun 26, 2020 at 08:45:22PM +0900, Stafford Horne wrote:

> I think we can make this unconditional.  The only reason I used the condition is
> because the only architecture that sets CONFIG_ARCH_TASK_STRUCT_ON_STACK is ia64
> which already has 64-bit alignment.
> 
> I can change it to (not incorporating max_t(ARCH_MIN_TASKALIGN) as not sure how):

The only arch that has ARCH_MIN_TASKALIGN larger than L1_CACHE_SIZE is
some daft x86 config and it shouldn't be fatal to them.

So with this:

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

> diff --git a/init/init_task.c b/init/init_task.c
> index 15089d15010a..ab6173f8e6a8 100644
> --- a/init/init_task.c
> +++ b/init/init_task.c
> @@ -65,6 +65,7 @@ struct task_struct init_task
>  #ifdef CONFIG_ARCH_TASK_STRUCT_ON_STACK
>         __init_task_data
>  #endif
> +       __aligned(L1_CACHE_BYTES)
>  = {
>  #ifdef CONFIG_THREAD_INFO_IN_TASK
>         .thread_info    = INIT_THREAD_INFO(init_task),
> 
> -Stafford

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-06-26 13:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-25 20:44 [PATCH] init: Align init_task to avoid conflict with MUTEX_FLAGS Stafford Horne
2020-06-26 11:12 ` Peter Zijlstra
2020-06-26 11:45   ` Stafford Horne
2020-06-26 13:11     ` Peter Zijlstra

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.