Skip to content

Commit 6aed82d

Browse files
Daniel LezcanoKAGA-KOKO
authored andcommitted
genirq/timings: Add selftest for circular array
Due to the complexity of the code and the difficulty to debug it, add some selftests to the framework in order to spot issues or regression at boot time when the runtime testing is enabled for this subsystem. This tests the circular buffer at the limits and validates: - the encoding / decoding of the values - the macro to browse the irq timings circular buffer - the function to push data in the circular buffer Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: andriy.shevchenko@linux.intel.com Link: https://lkml.kernel.org/r/20190527205521.12091-7-daniel.lezcano@linaro.org
1 parent 23aa3b9 commit 6aed82d

3 files changed

Lines changed: 130 additions & 0 deletions

File tree

‎kernel/irq/Makefile‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
obj-y := irqdesc.o handle.o manage.o spurious.o resend.o chip.o dummychip.o devres.o
44
obj-$(CONFIG_IRQ_TIMINGS) += timings.o
5+
ifeq ($(CONFIG_TEST_IRQ_TIMINGS),y)
6+
CFLAGS_timings.o += -DDEBUG
7+
endif
58
obj-$(CONFIG_GENERIC_IRQ_CHIP) += generic-chip.o
69
obj-$(CONFIG_GENERIC_IRQ_PROBE) += autoprobe.o
710
obj-$(CONFIG_IRQ_DOMAIN) += irqdomain.o

‎kernel/irq/timings.c‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// SPDX-License-Identifier: GPL-2.0
22
// Copyright (C) 2016, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
3+
#define pr_fmt(fmt) "irq_timings: " fmt
34

45
#include <linux/kernel.h>
56
#include <linux/percpu.h>
67
#include <linux/slab.h>
78
#include <linux/static_key.h>
9+
#include <linux/init.h>
810
#include <linux/interrupt.h>
911
#include <linux/idr.h>
1012
#include <linux/irq.h>
@@ -625,3 +627,120 @@ int irq_timings_alloc(int irq)
625627

626628
return 0;
627629
}
630+
631+
#ifdef CONFIG_TEST_IRQ_TIMINGS
632+
static int __init irq_timings_test_irqts(struct irq_timings *irqts,
633+
unsigned count)
634+
{
635+
int start = count >= IRQ_TIMINGS_SIZE ? count - IRQ_TIMINGS_SIZE : 0;
636+
int i, irq, oirq = 0xBEEF;
637+
u64 ots = 0xDEAD, ts;
638+
639+
/*
640+
* Fill the circular buffer by using the dedicated function.
641+
*/
642+
for (i = 0; i < count; i++) {
643+
pr_debug("%d: index=%d, ts=%llX irq=%X\n",
644+
i, i & IRQ_TIMINGS_MASK, ots + i, oirq + i);
645+
646+
irq_timings_push(ots + i, oirq + i);
647+
}
648+
649+
/*
650+
* Compute the first elements values after the index wrapped
651+
* up or not.
652+
*/
653+
ots += start;
654+
oirq += start;
655+
656+
/*
657+
* Test the circular buffer count is correct.
658+
*/
659+
pr_debug("---> Checking timings array count (%d) is right\n", count);
660+
if (WARN_ON(irqts->count != count))
661+
return -EINVAL;
662+
663+
/*
664+
* Test the macro allowing to browse all the irqts.
665+
*/
666+
pr_debug("---> Checking the for_each_irqts() macro\n");
667+
for_each_irqts(i, irqts) {
668+
669+
irq = irq_timing_decode(irqts->values[i], &ts);
670+
671+
pr_debug("index=%d, ts=%llX / %llX, irq=%X / %X\n",
672+
i, ts, ots, irq, oirq);
673+
674+
if (WARN_ON(ts != ots || irq != oirq))
675+
return -EINVAL;
676+
677+
ots++; oirq++;
678+
}
679+
680+
/*
681+
* The circular buffer should have be flushed when browsed
682+
* with for_each_irqts
683+
*/
684+
pr_debug("---> Checking timings array is empty after browsing it\n");
685+
if (WARN_ON(irqts->count))
686+
return -EINVAL;
687+
688+
return 0;
689+
}
690+
691+
static int __init irq_timings_irqts_selftest(void)
692+
{
693+
struct irq_timings *irqts = this_cpu_ptr(&irq_timings);
694+
int i, ret;
695+
696+
/*
697+
* Test the circular buffer with different number of
698+
* elements. The purpose is to test at the limits (empty, half
699+
* full, full, wrapped with the cursor at the boundaries,
700+
* wrapped several times, etc ...
701+
*/
702+
int count[] = { 0,
703+
IRQ_TIMINGS_SIZE >> 1,
704+
IRQ_TIMINGS_SIZE,
705+
IRQ_TIMINGS_SIZE + (IRQ_TIMINGS_SIZE >> 1),
706+
2 * IRQ_TIMINGS_SIZE,
707+
(2 * IRQ_TIMINGS_SIZE) + 3,
708+
};
709+
710+
for (i = 0; i < ARRAY_SIZE(count); i++) {
711+
712+
pr_info("---> Checking the timings with %d/%d values\n",
713+
count[i], IRQ_TIMINGS_SIZE);
714+
715+
ret = irq_timings_test_irqts(irqts, count[i]);
716+
if (ret)
717+
break;
718+
}
719+
720+
return ret;
721+
}
722+
723+
static int __init irq_timings_selftest(void)
724+
{
725+
int ret;
726+
727+
pr_info("------------------- selftest start -----------------\n");
728+
729+
/*
730+
* At this point, we don't except any subsystem to use the irq
731+
* timings but us, so it should not be enabled.
732+
*/
733+
if (static_branch_unlikely(&irq_timing_enabled)) {
734+
pr_warn("irq timings already initialized, skipping selftest\n");
735+
return 0;
736+
}
737+
738+
ret = irq_timings_irqts_selftest();
739+
740+
pr_info("---------- selftest end with %s -----------\n",
741+
ret ? "failure" : "success");
742+
743+
return ret;
744+
}
745+
early_initcall(irq_timings_selftest);
746+
#endif

‎lib/Kconfig.debug‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,6 +1858,14 @@ config TEST_PARMAN
18581858

18591859
If unsure, say N.
18601860

1861+
config TEST_IRQ_TIMINGS
1862+
bool "IRQ timings selftest"
1863+
depends on IRQ_TIMINGS
1864+
help
1865+
Enable this option to test the irq timings code on boot.
1866+
1867+
If unsure, say N.
1868+
18611869
config TEST_LKM
18621870
tristate "Test module loading with 'hello world' module"
18631871
depends on m

0 commit comments

Comments
 (0)