Robert Love's "Linux Kernel Development" book says that it is required that we pass the flags to the local_irq_save()
as a stack variable.
Why is this required? Is it okay to bypass this requirement in x86?
Robert Love's "Linux Kernel Development" book says that it is required that we pass the flags to the local_irq_save()
as a stack variable.
Why is this required? Is it okay to bypass this requirement in x86?
You probably refer to this quote from LKD3:
local_irq_save(flags); /* interrupts are now disabled */ /* ... */ local_irq_restore(flags); /* interrupts are restored to their previous state */
Note that these methods are implemented at least in part as macros, so the flags parameter (which must be defined as an
unsigned long
) is seemingly passed by value. This parameter contains architecture-specific data containing the state of the interrupt systems. Because at least one supported architecture incorporates stack information into the value (ahem, SPARC), flags cannot be passed to another function (specifically, it must remain on the same stack frame). For this reason, the call to save and the call to restore interrupts must occur in the same function.
I don't see any requirements for the flags
variable to be declared on stack.
What book says is:
flags
variable, when doing local_irq_save()
, along with interrupts informationflags
variable to another functionlocal_irq_save()
/ local_irq_restore()
in the same stack frameYou must be confused by this statement:
specifically, it must remain on the same stack frame
I'd change it a little bit:
specifically, it must remain on the same stack frame between save/restore calls
More than that, if you do:
$ git grep --all-match -e 'local_irq_save(\*' -- kernel/ include/linux/
you will see that even core kernel code declares flags
on heap, sometimes.
As for mentioned implementation on SPARC architecture: book probably refers to this code. So on SPARC architecture the flags
variable will contain PSR register, which in turn contains CWP
field, and it probably has to do with stack, somehow.
Is it okay to bypass this requirement in x86?
When you are writing architecture-independent code (like drivers), you should consider behavior on all architectures. So when using architecture-independent API, you shouldn't usually think about quirks on some particular platforms. But again, you can declare flags
on heap any time you want.