diff mbox

ARM: Add safe diagnostic to indicate when __cpu_architecture isn't set up

Message ID 1313504340-28004-1-git-send-email-dave.martin@linaro.org
State Superseded
Headers show

Commit Message

Dave Martin Aug. 16, 2011, 2:19 p.m. UTC
Although assing BUG() when __cpu_architecture is unexpectedly
CPU_ARCH_UNKNOWN seems like a good idea, in practice the kernel may
not be able to boot far enough even to write to the console in such
cases, even with earlyprintk enabled.

This patch adds a more useful diagnostic, and works out the cpu
architecture from scratch in such cases, instead of just returning
CPU_ARCH_UNKNOWN to the caller.

This patch is useful for debugging, but I'm not convinced it should
be merged.

Signed-off-by: Dave Martin <dave.martin@linaro.org>
---
 arch/arm/include/asm/system.h |    9 +++++++--
 arch/arm/kernel/setup.c       |    2 +-
 2 files changed, 8 insertions(+), 3 deletions(-)

Comments

Tixy Aug. 16, 2011, 2:45 p.m. UTC | #1
On Tue, 2011-08-16 at 15:19 +0100, Dave Martin wrote:
[...]
> This patch is useful for debugging, but I'm not convinced it should
> be merged.
[...] 
>  static inline int __pure cpu_architecture(void)
>  {
> -	BUG_ON(__cpu_architecture == CPU_ARCH_UNKNOWN);
> -	return __cpu_architecture;
> +	if (unlikely(__cpu_architecture == CPU_ARCH_UNKNOWN)) {
> +		extern int __pure __get_cpu_architecture(void);
> +
> +		WARN_ONCE(1, "__cpu_architecture not set yet!\n");
> +		return __get_cpu_architecture();
> +	} else
> +		return __cpu_architecture;
>  }

Seems to me that if we go down this route, cpu_architecture() may as
well remain a non-inline function which just calculates the arch if it's
not already set...

	if (unlikely(__cpu_architecture == CPU_ARCH_UNKNOWN))
		__cpu_architecture = __get_cpu_architecture();
	return __cpu_architecture;

There seems to be too many ways to skin this cat :-)
Nicolas Pitre Aug. 16, 2011, 2:59 p.m. UTC | #2
On Tue, 16 Aug 2011, Tixy wrote:

> On Tue, 2011-08-16 at 15:19 +0100, Dave Martin wrote:
> [...]
> > This patch is useful for debugging, but I'm not convinced it should
> > be merged.
> [...] 
> >  static inline int __pure cpu_architecture(void)
> >  {
> > -	BUG_ON(__cpu_architecture == CPU_ARCH_UNKNOWN);
> > -	return __cpu_architecture;
> > +	if (unlikely(__cpu_architecture == CPU_ARCH_UNKNOWN)) {
> > +		extern int __pure __get_cpu_architecture(void);
> > +
> > +		WARN_ONCE(1, "__cpu_architecture not set yet!\n");
> > +		return __get_cpu_architecture();
> > +	} else
> > +		return __cpu_architecture;
> >  }
> 
> Seems to me that if we go down this route, cpu_architecture() may as
> well remain a non-inline function which just calculates the arch if it's
> not already set...

My thought as well.

> 	if (unlikely(__cpu_architecture == CPU_ARCH_UNKNOWN))
> 		__cpu_architecture = __get_cpu_architecture();
> 	return __cpu_architecture;
> 
> There seems to be too many ways to skin this cat :-)

Agreed.


Nicolas
Dave Martin Aug. 16, 2011, 4:15 p.m. UTC | #3
On Tue, Aug 16, 2011 at 10:59:18AM -0400, Nicolas Pitre wrote:
> On Tue, 16 Aug 2011, Tixy wrote:
> 
> > On Tue, 2011-08-16 at 15:19 +0100, Dave Martin wrote:
> > [...]
> > > This patch is useful for debugging, but I'm not convinced it should
> > > be merged.
> > [...] 
> > >  static inline int __pure cpu_architecture(void)
> > >  {
> > > -	BUG_ON(__cpu_architecture == CPU_ARCH_UNKNOWN);
> > > -	return __cpu_architecture;
> > > +	if (unlikely(__cpu_architecture == CPU_ARCH_UNKNOWN)) {
> > > +		extern int __pure __get_cpu_architecture(void);
> > > +
> > > +		WARN_ONCE(1, "__cpu_architecture not set yet!\n");
> > > +		return __get_cpu_architecture();
> > > +	} else
> > > +		return __cpu_architecture;
> > >  }
> > 
> > Seems to me that if we go down this route, cpu_architecture() may as
> > well remain a non-inline function which just calculates the arch if it's
> > not already set...
> 
> My thought as well.
> 
> > 	if (unlikely(__cpu_architecture == CPU_ARCH_UNKNOWN))
> > 		__cpu_architecture = __get_cpu_architecture();
> > 	return __cpu_architecture;
> > 
> > There seems to be too many ways to skin this cat :-)
> 
> Agreed.

Well, I guess the flipside is, do we need this check at all?

Because __cpu_architecture is set really early, and because if
it's referenced too early, the kernel will either not boot or
we'll hit BUG(), it's hard to imaging such a failure going unnoticed.

So this check could simply be viewed as overkill, though it does
feel safer to check.

Any thoughts?

---Dave
Nicolas Pitre Aug. 16, 2011, 4:22 p.m. UTC | #4
On Tue, 16 Aug 2011, Dave Martin wrote:

> On Tue, Aug 16, 2011 at 10:59:18AM -0400, Nicolas Pitre wrote:
> > On Tue, 16 Aug 2011, Tixy wrote:
> > 
> > > On Tue, 2011-08-16 at 15:19 +0100, Dave Martin wrote:
> > > [...]
> > > > This patch is useful for debugging, but I'm not convinced it should
> > > > be merged.
> > > [...] 
> > > >  static inline int __pure cpu_architecture(void)
> > > >  {
> > > > -	BUG_ON(__cpu_architecture == CPU_ARCH_UNKNOWN);
> > > > -	return __cpu_architecture;
> > > > +	if (unlikely(__cpu_architecture == CPU_ARCH_UNKNOWN)) {
> > > > +		extern int __pure __get_cpu_architecture(void);
> > > > +
> > > > +		WARN_ONCE(1, "__cpu_architecture not set yet!\n");
> > > > +		return __get_cpu_architecture();
> > > > +	} else
> > > > +		return __cpu_architecture;
> > > >  }
> > > 
> > > Seems to me that if we go down this route, cpu_architecture() may as
> > > well remain a non-inline function which just calculates the arch if it's
> > > not already set...
> > 
> > My thought as well.
> > 
> > > 	if (unlikely(__cpu_architecture == CPU_ARCH_UNKNOWN))
> > > 		__cpu_architecture = __get_cpu_architecture();
> > > 	return __cpu_architecture;
> > > 
> > > There seems to be too many ways to skin this cat :-)
> > 
> > Agreed.
> 
> Well, I guess the flipside is, do we need this check at all?
> 
> Because __cpu_architecture is set really early, and because if
> it's referenced too early, the kernel will either not boot or
> we'll hit BUG(), it's hard to imaging such a failure going unnoticed.
> 
> So this check could simply be viewed as overkill, though it does
> feel safer to check.
> 
> Any thoughts?

I personally think this is overkill.


Nicolas
Dave Martin Aug. 17, 2011, 10:53 a.m. UTC | #5
On Tue, Aug 16, 2011 at 12:22:43PM -0400, Nicolas Pitre wrote:
> On Tue, 16 Aug 2011, Dave Martin wrote:
> 
> > On Tue, Aug 16, 2011 at 10:59:18AM -0400, Nicolas Pitre wrote:
> > > On Tue, 16 Aug 2011, Tixy wrote:
> > > 
> > > > On Tue, 2011-08-16 at 15:19 +0100, Dave Martin wrote:
> > > > [...]
> > > > > This patch is useful for debugging, but I'm not convinced it should
> > > > > be merged.
> > > > [...] 
> > > > >  static inline int __pure cpu_architecture(void)
> > > > >  {
> > > > > -	BUG_ON(__cpu_architecture == CPU_ARCH_UNKNOWN);
> > > > > -	return __cpu_architecture;
> > > > > +	if (unlikely(__cpu_architecture == CPU_ARCH_UNKNOWN)) {
> > > > > +		extern int __pure __get_cpu_architecture(void);
> > > > > +
> > > > > +		WARN_ONCE(1, "__cpu_architecture not set yet!\n");
> > > > > +		return __get_cpu_architecture();
> > > > > +	} else
> > > > > +		return __cpu_architecture;
> > > > >  }
> > > > 
> > > > Seems to me that if we go down this route, cpu_architecture() may as
> > > > well remain a non-inline function which just calculates the arch if it's
> > > > not already set...
> > > 
> > > My thought as well.
> > > 
> > > > 	if (unlikely(__cpu_architecture == CPU_ARCH_UNKNOWN))
> > > > 		__cpu_architecture = __get_cpu_architecture();
> > > > 	return __cpu_architecture;
> > > > 
> > > > There seems to be too many ways to skin this cat :-)
> > > 
> > > Agreed.
> > 
> > Well, I guess the flipside is, do we need this check at all?
> > 
> > Because __cpu_architecture is set really early, and because if
> > it's referenced too early, the kernel will either not boot or
> > we'll hit BUG(), it's hard to imaging such a failure going unnoticed.
> > 
> > So this check could simply be viewed as overkill, though it does
> > feel safer to check.
> > 
> > Any thoughts?
> 
> I personally think this is overkill.

OK, well since the inlining of cpu_architecture() doesn't feel like it
really brings us any benefits I may move it back out of line, into setup.c,
but keeping its new definition, including BUG().  The only reason for
moving that function to be inline was because it had become trivial, but
putting BUG() back undermines that.

I will also remove the declaration of __cpu_architecture from <asm/system.h>,
since for now this should not be used directly at all except by the undef
handler, and we don't want to encourage its use.

Any concerns on all of that?

In the meantime, I will re-roll those patches... hopefully for the final
time.

Cheers
---Dave
diff mbox

Patch

diff --git a/arch/arm/include/asm/system.h b/arch/arm/include/asm/system.h
index b0445f7..d679d2f 100644
--- a/arch/arm/include/asm/system.h
+++ b/arch/arm/include/asm/system.h
@@ -110,8 +110,13 @@  extern int __cpu_architecture;
 
 static inline int __pure cpu_architecture(void)
 {
-	BUG_ON(__cpu_architecture == CPU_ARCH_UNKNOWN);
-	return __cpu_architecture;
+	if (unlikely(__cpu_architecture == CPU_ARCH_UNKNOWN)) {
+		extern int __pure __get_cpu_architecture(void);
+
+		WARN_ONCE(1, "__cpu_architecture not set yet!\n");
+		return __get_cpu_architecture();
+	} else
+		return __cpu_architecture;
 }
 
 extern void cpu_init(void);
diff --git a/arch/arm/kernel/setup.c b/arch/arm/kernel/setup.c
index 1e0c1b3..188aae5 100644
--- a/arch/arm/kernel/setup.c
+++ b/arch/arm/kernel/setup.c
@@ -212,7 +212,7 @@  static const char *proc_arch[] = {
 	"?(17)",
 };
 
-static int __init __get_cpu_architecture(void)
+int __get_cpu_architecture(void)
 {
 	int cpu_arch;