Skip to contentSkip to conversation

genirq/matrix: Prevent managed_allocated/total_allocated counters getting out-of-sync

PATCH Applied Vitaly Kuznetsov 2 participants 2 messages Mar 19, 2021 IRQ

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 genirq/matrix: Prevent managed_allocated/total_allocated counters getting out-of-sync

When irq_matrix_free() is called for an unallocated interrupt, managed_allocated/total_allocated counters get out of sync with the real state of the matrix. Later, when the last interrupt is freed, these counters will go negative (overflow). While this is certainly a problem of the calling code, we can do better in irq_matrix_free() and simplify debugging.

An example of a problem described above:
https://lore.kernel.org/lkml/[email protected]/

---
 kernel/irq/matrix.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/irq/matrix.c +3 -1
index 651a4ad6d711..8e586858bcf4 100644
@@ -423,7 +423,9 @@ void irq_matrix_free(struct irq_matrix *m, unsigned int cpu,
 	if (WARN_ON_ONCE(bit < m->alloc_start || bit >= m->alloc_end))
 		return;
 
-	clear_bit(bit, cm->alloc_map);
+	if (WARN_ON_ONCE(!test_and_clear_bit(bit, cm->alloc_map)))
+		return;
+
 	cm->allocated--;
 	if(managed)
 		cm->managed_allocated--;
Signature · 1 line
2.30.2
#1 lore raw

[tip: irq/core] genirq/matrix: Prevent allocation counter corruption

The following commit has been merged into the irq/core branch of tip:

The following commit has been merged into the irq/core branch of tip:

Commit-ID:     c93a5e20c3c2dabef8ea360a3d3f18c6f68233ab
Gitweb:        https://git.kernel.org/tip/c93a5e20c3c2dabef8ea360a3d3f18c6f68233ab
Author:        Vitaly Kuznetsov <[email protected]>
AuthorDate:    Fri, 19 Mar 2021 12:18:23 +01:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Fri, 19 Mar 2021 22:52:11 +01:00

genirq/matrix: Prevent allocation counter corruption

When irq_matrix_free() is called for an unallocated vector the managed_allocated and total_allocated counters get out of sync with the real state of the matrix. Later, when the last interrupt is freed, these counters will underflow resulting in UINTMAX because the counters are unsigned.

While this is certainly a problem of the calling code, this can be catched in the allocator by checking the allocation bit for the to be freed vector which simplifies debugging.

An example of the problem described above:
https://lore.kernel.org/lkml/[email protected]/

Add the missing sanity check and emit a warning when it triggers.

---
 kernel/irq/matrix.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/irq/matrix.c +3 -1
index 6f8b1d1..578596e 100644
@@ -422,7 +422,9 @@ void irq_matrix_free(struct irq_matrix *m, unsigned int cpu,
 	if (WARN_ON_ONCE(bit < m->alloc_start || bit >= m->alloc_end))
 		return;
 
-	clear_bit(bit, cm->alloc_map);
+	if (WARN_ON_ONCE(!test_and_clear_bit(bit, cm->alloc_map)))
+		return;
+
 	cm->allocated--;
 	if(managed)
 		cm->managed_allocated--;