Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

irqs_disabled() vs in_interrupt() in linux

What is the difference between these two functions in Linux. I know that irqs_disabled() will return whether irqs are disabled or not and in_interrupt() will return whether you are in interrupt context or not. By default if you are in the interrupt context doesn't that mean irqs are disabled?

What are the scenarios where we use these functions specifically?

like image 363
Venkatesh Avatar asked Nov 05 '14 10:11

Venkatesh


2 Answers

Consider these two cases:

1) There are platform that support nested interrupts, where one interrupt can occur when another hasn't returned yet. The priorities are configured in the interrupt controller registers.

2) Multi core CPU can process two interrupts at the same time, on at each core.

There are many reasons to check if a function is running in interrupt context, i.e: functions that uses thread locking, shall not be executed in interrupt context otherwise a deadlock will occur. These functions might want to check if it is interrupt context and abort with an error.

Also, there are many reasons to disable interrupts, i.e.: when you are writing to a memory structure that will be consumed by an interrupt handler, you might fill up the data in atomic mode (irqs disabled).

One place where both functions are used is in the spinlock function family because spinlocks are good to be used in interrupt context.

like image 50
Felipe Lavratti Avatar answered Sep 21 '22 00:09

Felipe Lavratti


I got the answer.

#define in_irq()                (hardirq_count())
#define in_softirq()            (softirq_count())
#define in_interrupt()          (irq_count())

The function in_interrupt() checks both the (hard)irq and softirq counters. Either you executing an interrupt handler or a softirq handler, this function returns a true.

But irqs_disabled() checks only for (hard)irqs disabled or not.

like image 42
Venkatesh Avatar answered Sep 18 '22 00:09

Venkatesh