Skip to contentSkip to conversation

ledtrig netdev: what is the purpose of spinlock usage?

DISCUSSION Marek Behún 4 participants 6 messages Oct 29, 2020 LED

lore
Share & tools
lore
#0 lore raw

Hi Ben (or Pavel or whomever can explain this),

since you are the original author of netdev LED trigger, I guess this question should go to you. Why are spinlocks used as locks in the netdev trigger code? Is this for performance? Would it be a drastic performance hit to use mutexes?

The reason why I am asking is that I am designing an API for transparent HW offload of LED triggers. (Some LEDs, eg. LEDs on ethernet PHYs, can blink on rx/tx activity themselves).

You can find this at
https://git.kernel.org/pub/scm/linux/kernel/git/kabel/linux.git/log/?h=leds-trigger-hw-offload

The current approach for the netdev trigger is that the set_baseline_state function calls trigger_offload() method of the LED classdev. But the whole set_baseline_state function is called from within spinlock, and so when the trigger_offload method calls something that can sleep (MDIO bus communication, for example), kernel complains:

  BUG: scheduling while atomic

Thanks.

Marek

#1 lore raw

Hi!

Quoted 4 lines

since you are the original author of netdev LED trigger, I guess this question should go to you. Why are spinlocks used as locks in the netdev trigger code? Is this for performance? Would it be a drastic performance hit to use mutexes?

Triggers can be called from interrupt context, IIRC, and many simple LEDs can be operated from interrupt context, too.

Thus you need spinlocks...

Best regards,
Pavel

Signature · 1 line
http://www.livejournal.com/~pavelmachek
#2 lore raw
Quoted 7 lines · On Thu, 29 Oct 2020 18:45:29 +0100 Pavel Machek <[email protected]> wrote:

Hi!

Quoted 4 lines

since you are the original author of netdev LED trigger, I guess this question should go to you. Why are spinlocks used as locks in the netdev trigger code? Is this for performance? Would it be a drastic performance hit to use mutexes?

Quoted 7 lines

Triggers can be called from interrupt context, IIRC, and many simple LEDs can be operated from interrupt context, too.

Thus you need spinlocks...

Best regards,
Pavel

Pavel,

the set_baseline_state function in netdev trigger is guarded by a spinlock only when reading/writing device_name attribute and in netdev notify callback.

netdev_trig_notify can for example put the device away on NETDEV_UNREGISTER event, and if someone was reading/writing the device_name at the same time netdev_trig_notify is manipulating netdevice pointer, it could break. So my guess for the lock is that it is used because of this.

It is possible that netdev_trig_notify can be called from interrupt context, I will have to look into this.

Anyway for the trigger_offload() method to be able to communicate with PHYs I need the set_baseline_state function not to be called from within spinlock. Otherwise the drivers implementing this method would get too complicated. Would it be allright if on netdev event (link up, link down, netdev changename, netdev unregister) the set_baseline_state was called from a work, instead of directly from the spinlock?

I will send a patch proposing and explaining this.

Marek

#3 lore raw

Hi,

Quoted 25 lines · Marek Behún
Quoted 7 lines · On Thu, 29 Oct 2020 18:45:29 +0100 Pavel Machek <[email protected]> wrote:

Hi!

Quoted 4 lines

since you are the original author of netdev LED trigger, I guess this question should go to you. Why are spinlocks used as locks in the netdev trigger code? Is this for performance? Would it be a drastic performance hit to use mutexes?

Quoted 7 lines

Triggers can be called from interrupt context, IIRC, and many simple LEDs can be operated from interrupt context, too.

Thus you need spinlocks...

Best regards,
Pavel

Pavel,

the set_baseline_state function in netdev trigger is guarded by a spinlock only when reading/writing device_name attribute and in netdev notify callback.

Quoted 8 lines

netdev_trig_notify can for example put the device away on NETDEV_UNREGISTER event, and if someone was reading/writing the device_name at the same time netdev_trig_notify is manipulating netdevice pointer, it could break. So my guess for the lock is that it is used because of this.

It is possible that netdev_trig_notify can be called from interrupt context, I will have to look into this.

IIRC that is what I was seeing on my platform and I used the same locking mechanism for consistency, though my memory is hazy.

Quoted 9 lines

Anyway for the trigger_offload() method to be able to communicate with PHYs I need the set_baseline_state function not to be called from within spinlock. Otherwise the drivers implementing this method would get too complicated. Would it be allright if on netdev event (link up, link down, netdev changename, netdev unregister) the set_baseline_state was called from a work, instead of directly from the spinlock?

I will send a patch proposing and explaining this.

Go for it, patches welcome! Sounds interesting.

Quoted 2 lines

Marek

#4 lore raw
In reply to Marek Behún

Hi Marek,

Quoted 33 lines · Marek Behún
Quoted 7 lines · On Thu, 29 Oct 2020 18:45:29 +0100 Pavel Machek <[email protected]> wrote:

Hi!

Quoted 4 lines

since you are the original author of netdev LED trigger, I guess this question should go to you. Why are spinlocks used as locks in the netdev trigger code? Is this for performance? Would it be a drastic performance hit to use mutexes?

Quoted 7 lines

Triggers can be called from interrupt context, IIRC, and many simple LEDs can be operated from interrupt context, too.

Thus you need spinlocks...

Best regards,
Pavel

Pavel,

the set_baseline_state function in netdev trigger is guarded by a spinlock only when reading/writing device_name attribute and in netdev notify callback.

netdev_trig_notify can for example put the device away on NETDEV_UNREGISTER event, and if someone was reading/writing the device_name at the same time netdev_trig_notify is manipulating netdevice pointer, it could break. So my guess for the lock is that it is used because of this.

It is possible that netdev_trig_notify can be called from interrupt context, I will have to look into this.

Quoted 8 lines

Anyway for the trigger_offload() method to be able to communicate with PHYs I need the set_baseline_state function not to be called from within spinlock. Otherwise the drivers implementing this method would get too complicated. Would it be allright if on netdev event (link up, link down, netdev changename, netdev unregister) the set_baseline_state was called from a work, instead of directly from the spinlock?

I will send a patch proposing and explaining this.

register_netdevice_notifier() registers raw notifier chain, whose callbacks are not called from atomic context and there are no restrictions on callbacks. See include/linux/notifier.h.

So it looks like the spin_lock_bh() can be safely changed to mutex_lock().

Signature · 2 lines
Best regards,
Jacek Anaszewski
#5 lore raw
Quoted 7 lines · On Thu, 29 Oct 2020 23:49:19 +0100 Jacek Anaszewski…

register_netdevice_notifier() registers raw notifier chain, whose callbacks are not called from atomic context and there are no restrictions on callbacks. See include/linux/notifier.h.

So it looks like the spin_lock_bh() can be safely changed to mutex_lock().

Niiiiceeee, this simplifies things much. Thank you, Jacek :)