From 5e08ae4e14009d40ecd5f5428c6801eaeeffd125 Mon Sep 17 00:00:00 2001 From: Tom Date: Wed, 2 Dec 2020 09:41:52 -0700 Subject: [PATCH] Kernel: Fix counting interrupts Move counting interrupts out of the handle_interrupt method so that it is done in all cases without the interrupt handler having to implement it explicitly. Also make the counter an atomic value as e.g. the LocalAPIC interrupts may be triggered on multiple processors simultaneously. Fixes #4297 --- Kernel/Arch/i386/CPU.cpp | 8 +++++--- Kernel/Interrupts/GenericInterruptHandler.cpp | 5 +---- Kernel/Interrupts/GenericInterruptHandler.h | 9 ++++++--- Kernel/Interrupts/SharedIRQHandler.cpp | 1 - 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Kernel/Arch/i386/CPU.cpp b/Kernel/Arch/i386/CPU.cpp index 0de723d4612..eff69f96589 100644 --- a/Kernel/Arch/i386/CPU.cpp +++ b/Kernel/Arch/i386/CPU.cpp @@ -857,9 +857,11 @@ void handle_interrupt(TrapFrame* trap) auto& regs = *trap->regs; ASSERT(regs.isr_number >= IRQ_VECTOR_BASE && regs.isr_number <= (IRQ_VECTOR_BASE + GENERIC_INTERRUPT_HANDLERS_COUNT)); u8 irq = (u8)(regs.isr_number - 0x50); - ASSERT(s_interrupt_handler[irq]); - s_interrupt_handler[irq]->handle_interrupt(regs); - s_interrupt_handler[irq]->eoi(); + auto* handler = s_interrupt_handler[irq]; + ASSERT(handler); + handler->increment_invoking_counter(); + handler->handle_interrupt(regs); + handler->eoi(); } void enter_trap_no_irq(TrapFrame* trap) diff --git a/Kernel/Interrupts/GenericInterruptHandler.cpp b/Kernel/Interrupts/GenericInterruptHandler.cpp index 5fbcb3347e3..9e51cb78f0d 100644 --- a/Kernel/Interrupts/GenericInterruptHandler.cpp +++ b/Kernel/Interrupts/GenericInterruptHandler.cpp @@ -62,8 +62,5 @@ void GenericInterruptHandler::change_interrupt_number(u8 number) m_interrupt_number = number; register_generic_interrupt_handler(InterruptManagement::acquire_mapped_interrupt_number(interrupt_number()), *this); } -void GenericInterruptHandler::increment_invoking_counter() -{ - m_invoking_count++; -} + } diff --git a/Kernel/Interrupts/GenericInterruptHandler.h b/Kernel/Interrupts/GenericInterruptHandler.h index 991a1337ded..143bb87137f 100644 --- a/Kernel/Interrupts/GenericInterruptHandler.h +++ b/Kernel/Interrupts/GenericInterruptHandler.h @@ -48,7 +48,7 @@ public: u8 interrupt_number() const { return m_interrupt_number; } - size_t get_invoking_count() const { return m_invoking_count; } + size_t get_invoking_count() const { return m_invoking_count.load(AK::MemoryOrder::memory_order_relaxed); } virtual size_t sharing_devices_count() const = 0; virtual bool is_shared_handler() const = 0; @@ -59,7 +59,10 @@ public: virtual const char* controller() const = 0; virtual bool eoi() = 0; - void increment_invoking_counter(); + ALWAYS_INLINE void increment_invoking_counter() + { + m_invoking_count.fetch_add(1, AK::MemoryOrder::memory_order_relaxed); + } protected: void change_interrupt_number(u8 number); @@ -68,7 +71,7 @@ protected: void disable_remap() { m_disable_remap = true; } private: - size_t m_invoking_count { 0 }; + Atomic m_invoking_count { 0 }; u8 m_interrupt_number { 0 }; bool m_disable_remap { false }; }; diff --git a/Kernel/Interrupts/SharedIRQHandler.cpp b/Kernel/Interrupts/SharedIRQHandler.cpp index 59334012cde..6dee8047f93 100644 --- a/Kernel/Interrupts/SharedIRQHandler.cpp +++ b/Kernel/Interrupts/SharedIRQHandler.cpp @@ -87,7 +87,6 @@ SharedIRQHandler::~SharedIRQHandler() void SharedIRQHandler::handle_interrupt(const RegisterState& regs) { ASSERT_INTERRUPTS_DISABLED(); - increment_invoking_counter(); #ifdef INTERRUPT_DEBUG dbg() << "Interrupt @ " << interrupt_number(); dbg() << "Interrupt Handlers registered - " << m_handlers.size();