Skip to contentSkip to conversation

leds: ns2: Remove work queue

bug report Dan Carpenter 5 participants 6 messages Dec 10, 2021 – Dec 20, 2021 LED

lore
Share & tools
lore
#0 lore raw

Hello LED devs,

The patch c29e650b3af2: "leds: ns2: Remove work queue" from Nov 20, 2015, leads to the following Smatch static checker warning:

	drivers/leds/leds-ns2.c:96 ns2_led_set_mode()
	warn: sleeping in atomic context

drivers/leds/leds-ns2.c

    76 static void ns2_led_set_mode(struct ns2_led *led, enum ns2_led_modes mode)
    77 {
    78         int i;
    79         unsigned long flags;
    80 
    81         for (i = 0; i < led->num_modes; i++)
    82                 if (mode == led->modval[i].mode)
    83                         break;
    84 
    85         if (i == led->num_modes)
    86                 return;
    87 
    88         write_lock_irqsave(&led->rw_lock, flags);
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Holding a write lock (spin lock).

    89 
    90         if (!led->can_sleep) {
                    ^^^^^^^^^^^^^^^

Even if the led->can_sleep flag is set, we are not actually allowed to sleep when the preempt count is non-zero. Presumably we should make this unconditional.

    91                 gpiod_set_value(led->cmd, led->modval[i].cmd_level);
    92                 gpiod_set_value(led->slow, led->modval[i].slow_level);
    93                 goto exit_unlock;
    94         }
    95 
--> 96         gpiod_set_value_cansleep(led->cmd, led->modval[i].cmd_level);
    97         gpiod_set_value_cansleep(led->slow, led->modval[i].slow_level);
               ^^^^^^^^^^^^^^^^^^^^^^^^^

These functions can sleep.

    98 
    99 exit_unlock:
    100         write_unlock_irqrestore(&led->rw_lock, flags);
    101 }

regards,
dan carpenter

#1 lore raw

Hi!

Quoted 7 lines

Hello LED devs,

The patch c29e650b3af2: "leds: ns2: Remove work queue" from Nov 20, 2015, leads to the following Smatch static checker warning:

	drivers/leds/leds-ns2.c:96 ns2_led_set_mode()
	warn: sleeping in atomic context

Yup, this looks wrong.

Plus, the code is quite crazy.

Not sure what the write_lock in that function is supposed to protect against. Perhaps it can be just removed?

Hmm. led_set_mode uses custom interface for hardware accelerated LED. Ideally there's more fixing to be done there :-(.

Best regards,
Pavel

Signature · 1 line
http://www.livejournal.com/~pavelmachek
#2 lore raw
Quoted 12 lines · On Wed, 15 Dec 2021 21:39:55 +0100 Pavel Machek <[email protected]> wrote:

Hi!

Quoted 7 lines

Hello LED devs,

The patch c29e650b3af2: "leds: ns2: Remove work queue" from Nov 20, 2015, leads to the following Smatch static checker warning:

	drivers/leds/leds-ns2.c:96 ns2_led_set_mode()
	warn: sleeping in atomic context  

Yup, this looks wrong.

Quoted 7 lines

Plus, the code is quite crazy.

Not sure what the write_lock in that function is supposed to protect against. Perhaps it can be just removed?

Hmm. led_set_mode uses custom interface for hardware accelerated LED. Ideally there's more fixing to be done there :-(.

The last time we discussed this, Simon said that he is willing to convert once we have trigger offloading API. But we will also need blkdev trigger. Time to review Ian Pilcher's last attempt at blkdev?
[RESEND PATCH v8 0/2] Introduce block device LED trigger
https://lore.kernel.org/linux-leds/[email protected]/

Marek

#3 lore raw
Quoted 20 lines · Marek Behún
Quoted 17 lines · On Wed, 15 Dec 2021 21:39:55 +0100 Pavel Machek <[email protected]> wrote:

Hi!

Quoted 7 lines

Hello LED devs,

The patch c29e650b3af2: "leds: ns2: Remove work queue" from Nov 20, 2015, leads to the following Smatch static checker warning:

	drivers/leds/leds-ns2.c:96 ns2_led_set_mode()
	warn: sleeping in atomic context  

Yup, this looks wrong.

Plus, the code is quite crazy.

Not sure what the write_lock in that function is supposed to protect against. Perhaps it can be just removed?

Quoted 8 lines
Quoted 2 lines

Hmm. led_set_mode uses custom interface for hardware accelerated LED. Ideally there's more fixing to be done there :-(.

The last time we discussed this, Simon said that he is willing to convert once we have trigger offloading API. But we will also need blkdev trigger. Time to review Ian Pilcher's last attempt at blkdev?
[RESEND PATCH v8 0/2] Introduce block device LED trigger
https://lore.kernel.org/linux-leds/[email protected]/

Hi Marek,

I kind of remembering that :)

#4 lore raw
In reply to Marek Behún
Quoted 5 lines · Marek Behún

The last time we discussed this, Simon said that he is willing to convert once we have trigger offloading API. But we will also need blkdev trigger. Time to review Ian Pilcher's last attempt at blkdev?
[RESEND PATCH v8 0/2] Introduce block device LED trigger
https://lore.kernel.org/linux-leds/[email protected]/

I support this. ;-)

Let me know if you'd like me to resend, have any questions, etc.

Signature · 3 lines
========================================================================
                 In Soviet Russia, Google searches you!
========================================================================
#5 lore raw
In reply to Pavel Machek

Hi

Quoted 11 lines · Pavel Machek

Hi!

Quoted 7 lines

Hello LED devs,

The patch c29e650b3af2: "leds: ns2: Remove work queue" from Nov 20, 2015, leads to the following Smatch static checker warning:

	drivers/leds/leds-ns2.c:96 ns2_led_set_mode()
	warn: sleeping in atomic context

Yup, this looks wrong.

It's been a very long time since I watched this pilot or even the LED subsystem.

The "can_sleep" code seems to me quite the same as for the led_gpio driver. If can_sleep is set (because for example the GPIO controller uses I2C), then the brightness_set_blocking function should be used and then ns2_led_set() can't be called in atomic context. And also the gpiod_set_value_cansleep variants are used.

Where is it wrong ?

Quoted 2 lines

Plus, the code is quite crazy.

Well, there has been many changes since the version I wrote.

Quoted 3 lines

Not sure what the write_lock in that function is supposed to protect against. Perhaps it can be just removed?

At the time it was possible for brightness_set() to be called in user context (through the sysfs interface) or in interrupt/timer context (from a trigger). And because the LED is controlled through two GPIOs, then ns2_led_set_mode() needs to be protected against reentrancy. That's why the write_{lock_irqsave,unlock_irqrestore} functions have been used here.

I have no idea about what is the context situation now.

Quoted 3 lines

Hmm. led_set_mode uses custom interface for hardware accelerated LED. Ideally there's more fixing to be done there :-(.

At the time, nothing else was available and the LED API was crazy.

Simon