TransWikia.com

Why is irqs_disabled() an unsafe way of disabling preemption?

Unix & Linux Asked by Holmes.Sherlock on November 29, 2020

Proper Locking Under a Preemptible Kernel: Keeping Kernel Code Preempt-Safe article says,

But keep in mind that ‘irqs disabled’ is a fundamentally unsafe way of
disabling preemption – any spin_unlock() decreasing the preemption
count to 0 might trigger a reschedule. A simple printk() might trigger
a reschedule.

If IRQs are disabled, then the timer interrupt should be disabled on that CPU core as well. Naturally, it should disable the scheduler (preemption) in turn. Why is calling irqs disabled() an unsafe way to disable preemption?

Moreover, how might printk() trigger a reschedule?

2 Answers

Because timer interrupts (or interrupts in general) are not the only cause of kernel preemption. There are functions that can still trigger preemption (such as cond_resched()) which expect preemption to be explicitly disabled.

Answered by Matviy Kotoniy on November 29, 2020

Yes, if IRQs are disabled, the timer interrupt is disabled and task scheduling no longer occurs. The unsafe part is the “if”: if you’re relying on disabled IRQs, you need to be absolutely sure that all the code you run with IRQs disabled respects that. That can be quite difficult in the kernel since spinlocks disable and enable pre-emption themselves (along with IRQs in some cases), and many pieces of code use locks, including printk (to ensure that log messages don’t get mixed up). Any time a lock is released, you risk a reschedule (to run code that was waiting on the lock), even if IRQs are disabled: preempt_enable() explicitly calls __preempt_schedule() when its counter reaches zero, so no timer interrupt is required.

It’s thus safer to use the proper pre-emption support functions, especially in terms of future-proofing: you might be well aware of the constraints in the code you’re writing now, but someone else changing it won’t (and “someone else” includes “you in six months’ time”).

Answered by Stephen Kitt on November 29, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP