Skip to content

spin_lock_irqsave() / spin_unlock_irqrestore()

Purpose

Protects shared state that may be accessed from interrupt context.

#include <linux/spinlock.h>

Prototype

void spin_lock_irqsave(spinlock_t *lock, unsigned long flags);
void spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags);

Parameters

  • lock: pointer to an initialized spinlock.
  • flags: local interrupt state storage.

Return Value

  • No return value.

Execution Context

Spinlocks are intended for execution contexts that cannot sleep.

Typical usage includes:

  • Hard IRQ handlers
  • Shared data accessed by both Process Context and IRQ Context
  • Short critical sections in Process Context

When the same spinlock is shared between Process Context and IRQ Context, use spin_lock_irqsave() and spin_unlock_irqrestore().

Minimal Example

unsigned long flags;
spin_lock_irqsave(&dev->lock, flags);
/* update IRQ-shared state */
spin_unlock_irqrestore(&dev->lock, flags);

Common Pitfalls

  • Do not sleep while holding a spinlock.
  • Keep critical sections short.
  • Use the irqsave variant when the same lock may be used in interrupt context.