Re: [PATCH v9 3/3] leds: trigger: implement a tty trigger

From: Pavel Machek
Date: Tue Oct 20 2020 - 08:36:15 EST


Hi!

> Usage is as follows:
>
> myled=ledname
> tty=ttyS0
>
> echo tty > /sys/class/leds/$myled/trigger
> echo $tty > /sys/class/leds/$myled/ttyname
>
> . When this new trigger is active it periodically checks the tty's
> statistics and when it changed since the last check the led is flashed
> once.
>
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@xxxxxxxxxxxxxx>
> ---

> +static ssize_t ttyname_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + struct ledtrig_tty_data *trigger_data = led_trigger_get_drvdata(dev);
> + ssize_t len = 0;

Unused value. Not sure if some checker will complain.

> +static ssize_t ttyname_store(struct device *dev,
> + struct device_attribute *attr, const char *buf,
> + size_t size)
> +{
> + struct ledtrig_tty_data *trigger_data = led_trigger_get_drvdata(dev);
> + char *ttyname;
> + ssize_t ret = size;

> + ledtrig_tty_halt(trigger_data);
> +
> + mutex_lock(&trigger_data->mutex);
> +
> + if (size > 0 && buf[size - 1] == '\n')
> + size -= 1;
> +
> + if (size) {
> + ttyname = kmemdup_nul(buf, size, GFP_KERNEL);
> + if (!ttyname) {
> + ret = -ENOMEM;
> + goto out_unlock;

If this happens, you return error to the user and
trigger_data->ttyname remains with the old value, but trigger is now
stopped. That is not exactly consistent.


> +static void ledtrig_tty_work(struct work_struct *work)
> +{
> + struct ledtrig_tty_data *trigger_data =
> + container_of(work, struct ledtrig_tty_data, dwork.work);
> + struct serial_icounter_struct icount;
> + int ret;
> +
> + mutex_lock(&trigger_data->mutex);
> +
> + BUG_ON(!trigger_data->ttyname);
> +
> + /* try to get the tty corresponding to $ttyname */
> + if (!trigger_data->tty) {
> + dev_t devno;
> + struct tty_struct *tty;
> + int ret;
> +
> + ret = tty_dev_name_to_number(trigger_data->ttyname, &devno);
> + if (ret < 0)
> + /*
> + * A device with this name might appear later, so keep
> + * retrying.
> + */
> + goto out;

Poll every 100 msec... Hmm.... Okay, I guess?

> + ret = tty_get_icount(trigger_data->tty, &icount);
> + if (ret) {
> + mutex_unlock(&trigger_data->mutex);
> + dev_info(trigger_data->tty->dev, "Failed to get icount, stopped polling\n");
> + mutex_unlock(&trigger_data->mutex);

Eh?

> + return;
> + }
> +
> + if (icount.rx != trigger_data->rx ||
> + icount.tx != trigger_data->tx) {
> + led_set_brightness(trigger_data->led_cdev, LED_ON);

Are you sure about LED_ON here? It should use current brightness
selected by brightness file...

> +static int ledtrig_tty_activate(struct led_classdev *led_cdev)
> +{
> + struct ledtrig_tty_data *trigger_data;
> +
> + trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
> + if (!trigger_data)
> + return -ENOMEM;
> +
> + led_set_trigger_data(led_cdev, trigger_data);
> +
> + INIT_DELAYED_WORK(&trigger_data->dwork, ledtrig_tty_work);
> + trigger_data->led_cdev = led_cdev;
> + mutex_init(&trigger_data->mutex);

How is this protected from concurrent access from sysfs?

Best regards,
Pavel

--
http://www.livejournal.com/~pavelmachek

Attachment: signature.asc
Description: PGP signature