Re: When to save/restore_flags() vs cli/sti()

Doug Ledford (
Thu, 25 Feb 1999 15:22:52 -0500

"B. James Phillippe" wrote:
> On Thu, 25 Feb 1999, Alan Cox wrote:
> > > I'd like to know more details on when exactly it is safe to use the simpler
> > > cli()/sti() combination over the save_flags()/restore_flags().
> >
> > When you know interrupts were previously enabled and also know nobody will
> > ever call the function now or in the future with interrupts disabled. In
> > general I think "don't do it" is the answer.
> Okay, I feel better now. I have only ever used save_flags() but I worried
> I might be being to anal. Does this leave cli()/sti() to only be
> appropriate within functions that will only be invoked as part of a system
> call then? Such as device driver open/read/etc?

Just becuase I've not seen this mentioned and I'm being anal here for people
reading this thread thay may not know this. save_flags(flags); by itself will
not do anything truly interesting. You really need this construct:

save_flags(flags); /* save the current cpu flags */
cli(); /* actually turn interrupts off */
... do stuff
restore_flags(flags); /* restore the old cpu state */

And, of course, the purpose of that is since you save the state and then turn
interrupts off, you code is safe, and whether it was called with interrupts
already off or still on doesn't matter because the state will get set back to
exactly what it was before you executed your protected code.

  Doug Ledford   <>
   Opinions expressed are my own, but
      they should be everybody's.

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to Please read the FAQ at