mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Bartosz Golaszewski <brgl@bgdev.pl>
Cc: Linus Walleij <linus.walleij@linaro.org>,
	Kent Gibson <warthog618@gmail.com>,
	Viresh Kumar <viresh.kumar@linaro.org>,
	Geert Uytterhoeven <geert@linux-m68k.org>,
	linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org,
	Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Subject: Re: [PATCH v3] gpio: consumer: new virtual driver
Date: Thu, 10 Aug 2023 18:03:43 +0300	[thread overview]
Message-ID: <ZNT8T1n0IeJ1uzl6@smile.fi.intel.com> (raw)
In-Reply-To: <20230809142709.28042-1-brgl@bgdev.pl>

On Wed, Aug 09, 2023 at 04:27:09PM +0200, Bartosz Golaszewski wrote:
> 
> The GPIO subsystem has a serious problem with undefined behavior and
> use-after-free bugs on hot-unplug of GPIO chips. This can be considered a
> corner-case by some as most GPIO controllers are enabled early in the
> boot process and live until the system goes down but most GPIO drivers
> do allow unbind over sysfs, many are loadable modules that can be (force)
> unloaded and there are also GPIO devices that can be dynamically detached,
> for instance CP2112 which is a USB GPIO expender.
> 
> Bugs can be triggered both from user-space as well as by in-kernel users.
> We have the means of testing it from user-space via the character device
> but the issues manifest themselves differently in the kernel.
> 
> This is a proposition of adding a new virtual driver - a configurable
> GPIO consumer that can be configured over configfs (similarly to
> gpio-sim).
> 
> The configfs interface allows users to create dynamic GPIO lookup tables
> that are registered with the GPIO subsystem. Every config group
> represents a consumer device. Every sub-group represents a single GPIO
> lookup. The device can work in three modes: just keeping the line
> active, toggling it every second or requesting its interrupt and
> reporting edges. Every lookup allows to specify the key, offset and
> flags as per the lookup struct defined in linux/gpio/machine.h.
> 
> The module together with gpio-sim allows to easily trigger kernel
> hot-unplug errors. A simple use-case is to create a simulated chip,
> setup the consumer to lookup one of its lines in 'monitor' mode, unbind
> the simulator, unbind the consumer and observe the fireworks in dmesg.
> 
> This driver is aimed as a helper in tackling the hot-unplug problem in
> GPIO as well as basis for future regression testing once the fixes are
> upstream.

...

> +static void gpio_consumer_on_timer(struct timer_list *timer)
> +{
> +	struct gpio_consumer_timer_data *timer_data = to_timer_data(timer);
> +
> +	timer_data->val = timer_data->val ? 1 : 0;

I guess it should be 0 : 1.

> +	gpiod_set_value_cansleep(timer_data->desc, timer_data->val);
> +	mod_timer(&timer_data->timer, jiffies + msecs_to_jiffies(1000));
> +}

...

> +static ssize_t
> +gpio_consumer_lookup_config_key_show(struct config_item *item, char *page)
> +{
> +	struct gpio_consumer_lookup *lookup = to_gpio_consumer_lookup(item);
> +	struct gpio_consumer_device *dev = lookup->parent;

> +	int ret;

Why is it needed now? Seems you were too fast to send v3, look at my comments
in v2 thread.

> +	scoped_guard(mutex, &dev->lock)
> +		ret = sprintf(page, "%s\n", lookup->key);
> +
> +	return ret;
> +}

...

> +static ssize_t
> +gpio_consumer_lookup_config_key_store(struct config_item *item,
> +				      const char *page, size_t count)
> +{
> +	struct gpio_consumer_lookup *lookup = to_gpio_consumer_lookup(item);
> +	struct gpio_consumer_device *dev = lookup->parent;
> +	char *key __free(kfree) = NULL;
> +	char *stripped;
> +
> +	key = kstrndup(page, count, GFP_KERNEL);

skip_spaces() will allow you to get rid of memmove().

> +	if (!key)
> +		return -ENOMEM;
> +
> +	stripped = strstrip(key);
> +	memmove(key, stripped, strlen(stripped) + 1);

And this become something like

	/* Get rid of trailing newline and spaces */
	strim(key);

> +	guard(mutex)(&dev->lock);
> +
> +	if (gpio_consumer_device_is_live_unlocked(dev))
> +		return -EBUSY;
> +
> +	kfree(lookup->key);
> +	lookup->key = no_free_ptr(key);
> +
> +	return count;
> +}

...

> +static enum gpio_lookup_flags
> +gpio_consumer_lookup_get_flags(struct config_item *item)
> +{
> +	struct gpio_consumer_lookup *lookup = to_gpio_consumer_lookup(item);
> +	struct gpio_consumer_device *dev = lookup->parent;
> +	enum gpio_lookup_flags flags;
> +
> +	scoped_guard(mutex, &dev->lock)

> +		flags = lookup->flags;
> +
> +	return flags;

	guard()
	return lookup->flags;

?

> +}

...

> +static ssize_t
> +gpio_consumer_device_config_live_store(struct config_item *item,
> +				       const char *page, size_t count)
> +{
> +	struct gpio_consumer_device *dev = to_gpio_consumer_device(item);
> +	bool live;
> +	int ret;
> +
> +	ret = kstrtobool(page, &live);
> +	if (ret)
> +		return ret;
> +
> +	guard(mutex)(&dev->lock);
> +
> +	if (live == gpio_consumer_device_is_live_unlocked(dev))
> +		ret = -EPERM;

		return ... ?

> +	else if (live)

	if () ?

> +		ret = gpio_consumer_device_activate_unlocked(dev);

> +	else

drop it ?

> +		gpio_consumer_device_deactivate_unlocked(dev);
> +
> +	return ret ?: count;
> +}

-- 
With Best Regards,
Andy Shevchenko



  reply	other threads:[~2023-08-10 15:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-09 14:27 Bartosz Golaszewski
2023-08-10 15:03 ` Andy Shevchenko [this message]
2023-08-11 14:15   ` Bartosz Golaszewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZNT8T1n0IeJ1uzl6@smile.fi.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=bartosz.golaszewski@linaro.org \
    --cc=brgl@bgdev.pl \
    --cc=geert@linux-m68k.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=viresh.kumar@linaro.org \
    --cc=warthog618@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®