x86, bts: add selftest for BTS
PATCH 1/4 x86, bts: add selftest for BTS
Add code to test the branch trace store functionality when a cpu is initialized.
If an error is detected, a WARN()ing is issued and bts is disabled.
---
arch/x86/kernel/ds.c +226 -2
@@ -926,8 +926,217 @@ static const struct ds_configuration ds_ .sizeof_rec[ds_pebs] = 8 * 18, }; -static void -ds_configure (const struct ds_configuration *cfg) +#ifdef CONFIG_X86_DS_SELFTEST +static int ds_selftest_bts_consistency (const struct bts_trace *trace) +{ +int error =0 ; + +if (!trace) { +printk (KERN_CONT "failed to access trace..." ); +/* Bail out. Other tests are pointless. */ +return -1 ; + } + +if (!trace->read) { +printk (KERN_CONT "bts read not available..." ); + error =-1 ; + } + +/* Do some sanity checks on the trace configuration. */ +if (!trace->ds.n) { +printk (KERN_CONT "empty bts buffer..." ); + error =-1 ; + } +if (!trace->ds.size) { +printk (KERN_CONT "bad bts trace setup..." ); + error =-1 ; + } +if (trace->ds.end != + (char *) trace->ds.begin + (trace->ds.n * trace->ds.size)) { +printk (KERN_CONT "bad bts buffer setup..." ); + error =-1 ; + } +if ((trace->ds.top < trace->ds.begin) || + (trace->ds.end <= trace->ds.top)) { +printk (KERN_CONT "bts top out of bounds..." ); + error =-1 ; + } + +return error; +} + +static int ds_selftest_bts_read (struct bts_tracer *tracer, +const struct bts_trace *trace, +const void *from,const void *to) +{ +const unsigned char *at; + +/* Check a few things which do not belong to this test. +* They should be covered by other tests. */ +if (!trace) +return -1 ; + +if (!trace->read) +return -1 ; + +if (to < from) +return -1 ; + +if (from < trace->ds.begin) +return -1 ; + +if (trace->ds.end < to) +return -1 ; + +if (!trace->ds.size) +return -1 ; + +/* Now to the test itself. */ +for (at = from; (void *) at < to; at += trace->ds.size) { +struct bts_struct bts; +size_t index; +int error; + +if (((void *) at - trace->ds.begin) % trace->ds.size) { +printk (KERN_CONT +"read from non-integer index..." ); +return -1 ; + } + index = ((void *) at - trace->ds.begin) / trace->ds.size; + +memset (&bts,0 ,sizeof (bts)); + error = trace->read (tracer, at, &bts); +if (error <0 ) { +printk (KERN_CONT +"error reading bts trace at [%lu] (0x%p)..." , + index, at); +return error; + } + +switch (bts.qualifier) { +case BTS_BRANCH : +break ; +default : +printk (KERN_CONT +"unexpected bts entry %llu at [%lu] (0x%p)..." , + bts.qualifier, index, at); +return -1 ; + } + } + +return 0 ; +} + +#define DS_SELFTEST_SIZEOF_BUFFER 1021/* intentionally chose an odd size */ +static int ds_selftest_bts (void ) +{ +const struct bts_trace *trace; +struct bts_tracer *tracer; +int error =0 ; +void *top; +unsigned char buffer[DS_SELFTEST_SIZEOF_BUFFER ]; + + tracer = +ds_request_bts (/* task = */ NULL , buffer, +DS_SELFTEST_SIZEOF_BUFFER , +/* ovfl = */ NULL ,/* th = */ (size_t )-1 , +BTS_KERNEL ); +if (IS_ERR (tracer)) { + error =PTR_ERR (tracer); + tracer =NULL ; + +printk (KERN_CONT +"initialization failed (err: %d)..." , error); + goto out; + } + +/* The return from the initialization call should already give +* us enough trace. */ +ds_suspend_bts (tracer); + +/* Let's see if we can access the trace. */ + trace =ds_read_bts (tracer); + + error =ds_selftest_bts_consistency (trace); +if (error <0 ) + goto out; + +/* If everything went well, we should have a few trace entries. */ +if (trace->ds.top == trace->ds.begin) { +/* It is possible but highly unlikely that we got a +* buffer overflow and end up at exactly the same +* position we started from. +* Let's issue a warning, but continue. */ +printk (KERN_CONT "no trace/overflow..." ); + } + +/* Let's try to read the trace we collected. */ + error =ds_selftest_bts_read (tracer, trace, + trace->ds.begin, trace->ds.top); +if (error <0 ) + goto out; + +/* Let's read the trace again. Since we suspended tracing, we should +* get the same result. */ + top = trace->ds.top; + + trace =ds_read_bts (tracer); + error =ds_selftest_bts_consistency (trace); +if (error <0 ) + goto out; + +if (top != trace->ds.top) { +printk (KERN_CONT "suspend not working..." ); + error =-1 ; + goto out; + } + +/* Let's collect some more trace - see if resume is working. */ +ds_resume_bts (tracer); +ds_suspend_bts (tracer); + + trace =ds_read_bts (tracer); + + error =ds_selftest_bts_consistency (trace); +if (error <0 ) + goto out; + +if (trace->ds.top == top) { +/* It is possible but highly unlikely that we got a +* buffer overflow and end up at exactly the same +* position we started from. +* Let's issue a warning and check the full trace. */ +printk (KERN_CONT +"no resume progress/overflow..." ); + + error =ds_selftest_bts_read (tracer, trace, + trace->ds.begin, trace->ds.end); + }else if (trace->ds.top < top) { +/* We had a buffer overflow - the entire buffer should +* contain trace records. */ + error =ds_selftest_bts_read (tracer, trace, + trace->ds.begin, trace->ds.end); + }else { +/* It is quite likely that the buffer did not +* overflow. Let's just check the delta trace. */ + error =ds_selftest_bts_read (tracer, trace, + top, trace->ds.top); + } +if (error <0 ) + goto out; + + error =0 ; + +/* The final test: release the tracer while tracing is suspended. */ + out: +ds_release_bts (tracer); + +return error; +} +#endif /* CONFIG_X86_DS_SELFTEST */ + + +static void ds_configure (const struct ds_configuration *cfg) {memset (&ds_cfg,0 ,sizeof (ds_cfg)); ds_cfg = *cfg;
@@ -942,6 +1151,21 @@ ds_configure(const struct ds_configuratiprintk (KERN_INFO "[ds] pebs not available\n" );WARN_ON_ONCE (MAX_SIZEOF_DS < (12 * ds_cfg.sizeof_field)); + +#ifdef CONFIG_X86_DS_SELFTEST +if (ds_cfg.ctl[dsf_bts]) { +int error; + +printk (KERN_INFO "[ds] bts selftest..." ); + error =ds_selftest_bts (); +printk (KERN_CONT "%s.\n" , (error ?"failed" :"passed" )); + +if (error) { +WARN (1 ,"[ds] selftest failed. disabling bts.\n" ); + ds_cfg.ctl[dsf_bts] =0 ; + } + } +#endif /* CONFIG_X86_DS_SELFTEST */ }void __cpuinit ds_init_intel (struct cpuinfo_x86 *c)
arch/x86/Kconfig.debug +9 -0
@@ -176,6 +176,15 @@ config IOMMU_LEAK Add a simple leak tracer to the IOMMU code. This is useful when you are debugging a buggy device driver that leaks IOMMU mappings. +config X86_DS_SELFTEST + bool "DS selftest" + default y + depends on DEBUG_KERNEL + depends on X86_DS + ---help--- + Perform Debug Store selftests at boot time. + If in doubt, say "N". + config HAVE_MMIOTRACE_SUPPORT def_bool y
---------------------------------------------------------------------
Intel GmbH
Dornacher Strasse 1
85622 Feldkirchen/Muenchen Germany
Sitz der Gesellschaft: Feldkirchen bei Muenchen
Geschaeftsfuehrer: Douglas Lusk, Peter Gleissner, Hannes Schwaderer
Registergericht: Muenchen HRB 47456 Ust.-IdNr.
VAT Registration No.: DE129385895
Citibank Frankfurt (BLZ 502 109 00) 600119052
This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.
arch/x86/kernel/ds.c static const struct ds_configuration ds_ · line 930
Quoted 2 lines · Markus Metzger
arch/x86/kernel/ds.c +2
-0+#ifdef CONFIG_X86_DS_SELFTEST +static int ds_selftest_bts_consistency (const struct bts_trace *trace)
Consider putting this into ds_selftest.c instead of adding a large #ifdef block to ds.c.
arch/x86/kernel/ds.c static const struct ds_configuration ds_ · line 955
Quoted 2 lines
arch/x86/kernel/ds.c +2
-0+if (trace->ds.end != + (char *) trace->ds.begin + (trace->ds.n * trace->ds.size)) {
That extra space character after the type cast is not needed.
arch/x86/kernel/ds.c static const struct ds_configuration ds_ · line 975
Quoted 2 lines
arch/x86/kernel/ds.c +2
-0+/* Check a few things which do not belong to this test. +* They should be covered by other tests. */
Please use the customary comment style of:
/*
* Comment .....
* ...... goes here:
*/
arch/x86/kernel/ds.c static const struct ds_configuration ds_ · line 1005
Quoted 1 line
arch/x86/kernel/ds.c +1
-0+ index = ((void *) at - trace->ds.begin) / trace->ds.size;
cast.
arch/x86/kernel/ds.c static const struct ds_configuration ds_ · line 1031
Quoted 2 lines
arch/x86/kernel/ds.c +2
-0+#define DS_SELFTEST_SIZEOF_BUFFER 1021/* intentionally chose an odd size */ +static int ds_selftest_bts (void )
We put newlines after definitions.
Also, DS_SELFTEST_SIZEOF_BUFFER is an odd name - why not DS_SELFTEST_BUFFER_SIZE ?
arch/x86/kernel/ds.c static const struct ds_configuration ds_ · line 1043
Quoted 5 lines
arch/x86/kernel/ds.c +5
-0+ tracer = +ds_request_bts (/* task = */ NULL , buffer, +DS_SELFTEST_SIZEOF_BUFFER , +/* ovfl = */ NULL ,/* th = */ (size_t )-1 , +BTS_KERNEL );
that looks weird. Why not:
tracer = ds_request_bts(NULL, buffer, DS_SELFTEST_BUFFER_SIZE, NULL, (size_t)-1, BTS_KERNEL);
arch/x86/kernel/ds.c static const struct ds_configuration ds_ · line 1054
Quoted 2 lines
arch/x86/kernel/ds.c +2
-0+/* The return from the initialization call should already give +* us enough trace. */
Comment style.
arch/x86/kernel/ds.c static const struct ds_configuration ds_ · line 1069
Quoted 4 lines
arch/x86/kernel/ds.c +4
-0+/* It is possible but highly unlikely that we got a +* buffer overflow and end up at exactly the same +* position we started from. +* Let's issue a warning, but continue. */
ditto.
arch/x86/kernel/ds.c static const struct ds_configuration ds_ · line 1080
Quoted 2 lines
arch/x86/kernel/ds.c +2
-0+/* Let's read the trace again. Since we suspended tracing, we should +* get the same result. */
ditto.
arch/x86/kernel/ds.c static const struct ds_configuration ds_ · line 1108
Quoted 4 lines
arch/x86/kernel/ds.c +4
-0+/* It is possible but highly unlikely that we got a +* buffer overflow and end up at exactly the same +* position we started from. +* Let's issue a warning and check the full trace. */
ditto.
arch/x86/kernel/ds.c static const struct ds_configuration ds_ · line 1116
Quoted 2 lines
arch/x86/kernel/ds.c +2
-0+/* We had a buffer overflow - the entire buffer should +* contain trace records. */
ditto.
arch/x86/kernel/ds.c static const struct ds_configuration ds_ · line 1121
Quoted 2 lines
arch/x86/kernel/ds.c +2
-0+/* It is quite likely that the buffer did not +* overflow. Let's just check the delta trace. */
ditto.
Quoted 4 lines
arch/x86/kernel/ds.c +4
-0+#ifdef CONFIG_X86_DS_SELFTEST +if (ds_cfg.ctl[dsf_bts]) { +int error; +
arch/x86/kernel/ds.c ds_configure(const struct ds_configurati · line 1168
Quoted 10 lines
arch/x86/kernel/ds.c +10
-0+printk (KERN_INFO "[ds] bts selftest..." ); + error =ds_selftest_bts (); +printk (KERN_CONT "%s.\n" , (error ?"failed" :"passed" )); + +if (error) { +WARN (1 ,"[ds] selftest failed. disabling bts.\n" ); + ds_cfg.ctl[dsf_bts] =0 ; + } + } +#endif /* CONFIG_X86_DS_SELFTEST */
This bit should be in a helper function i guess - so the ugly #ifdef goes out of a function.
Thanks,
Ingo
Quoted 11 lines
-----Original Message-----
From: Ingo Molnar [mailto:[email protected]]
Sent: Friday, March 06, 2009 3:33 PM
To: Metzger, Markus Tarch/x86/kernel/ds.c static const struct ds_configuration ds_ · line 930
Quoted 2 lines
arch/x86/kernel/ds.c +2
-0+#ifdef CONFIG_X86_DS_SELFTEST +static int ds_selftest_bts_consistency (const struct bts_trace *trace)Consider putting this into
ds_selftest.cinstead of adding a large #ifdef block tods.c.
Thanks for your comments.
I will correct the style problems, move this into ds_selftest.c, and resend the entire series next week.
regards,
markus.
---------------------------------------------------------------------
Intel GmbH
Dornacher Strasse 1
85622 Feldkirchen/Muenchen Germany
Sitz der Gesellschaft: Feldkirchen bei Muenchen
Geschaeftsfuehrer: Douglas Lusk, Peter Gleissner, Hannes Schwaderer
Registergericht: Muenchen HRB 47456 Ust.-IdNr.
VAT Registration No.: DE129385895
Citibank Frankfurt (BLZ 502 109 00) 600119052
This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.
Quoted 13 lines · Metzger, Markus T
Quoted 11 lines
-----Original Message-----
From: Ingo Molnar [mailto:[email protected]]
Sent: Friday, March 06, 2009 3:33 PM
To: Metzger, Markus Tarch/x86/kernel/ds.c static const struct ds_configuration ds_ · line 930
Quoted 2 lines
arch/x86/kernel/ds.c +2
-0+#ifdef CONFIG_X86_DS_SELFTEST +static int ds_selftest_bts_consistency (const struct bts_trace *trace)Consider putting this into
ds_selftest.cinstead of adding a large #ifdef block tods.c.
Quoted 4 lines
Thanks for your comments.
I will correct the style problems, move this into
ds_selftest.c, and resend the entire series next week.
Thanks! I picked up the .29 items, so there's no rush.
Ingo