gpio: consumer: new virtual driver
Subsystems (2)
PATCH v4 1/1 gpio: consumer: new virtual driver
From: Bartosz Golaszewski <[email protected]>
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.
---
RFC -> v2:
- add documentation
- fix various issues pointed out by Andy: use struct_size() where applicable, improve the logic when storing the 'live' property, improve log messages, remove commas in terminators, etc.
v2 -> v3:
- use cleanup.h interfaces
- add some clarifying commets
- more minor code tweaks
v3 -> v4:
- fix the toggle value assignment
- use guard(mutex)() wherever we can return directly from the subsequent function call
- use skip_spaces() + strim() to avoid having to do a memmove() when stripping strings off whitespaces
- DON'T try to save a couple LOC in ifdefs if that makes them less readable (Andy :) )
.../admin-guide/gpio/gpio-consumer.rst | 111 ++ drivers/gpio/Kconfig | 7 + drivers/gpio/Makefile | 1 + drivers/gpio/gpio-consumer.c | 954 ++++++++++++++++++ 4 files changed, 1073 insertions(+) create mode 100644 Documentation/admin-guide/gpio/gpio-consumer.rst create mode 100644 drivers/gpio/gpio-consumer.c
Documentation/admin-guide/gpio/gpio-consumer.rst new file +111 -0
@@ -0,0 +1,111 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +Virtual GPIO Consumer +===================== + +The virtual GPIO Consumer module allows users to create dynamic GPIO lookup +tables using the exposed configfs interface and instantiate virtual GPIO +consumer devices that retrieve and use GPIOs using these tables. + +Creating GPIO consumers +----------------------- + +The gpio-consumer module registers a configfs subsystem called +``'gpio-consumer'``. For details of the configfs filesystem, please refer to +the configfs documentation. + +The user can create a hierarchy of configfs groups and items as well as modify +values of exposed attributes. Once the consumer is instantiated, this hierarchy +will be translated to appropriate device properties. The general structure is: + +**Group:** ``/config/gpio-consumer`` + +This is the top directory of the gpio-consumer configfs tree. + +**Group:** ``/config/gpio-consumer/consumer-device`` + +**Attribute:** ``/config/gpio-consumer/gpio-device/function`` + +**Attribute:** ``/config/gpio-consumer/gpio-device/live`` + +This is a directory representing a GPIO consumer device. The ``'function'`` +attribute defines how the device will use requested GPIOs. It takes the +following values: ``'active'`` - GPIOs will be requested as output-high and +kept in this state until the device is unbound, ``'toggle'`` - GPIOs will +be requested in output mode and then toggled periodically (every second) +between 1 and 0, ``'monitor'`` - GPIOs will be requested in input mode, then +requested as interrupts and their firing reported in the kernel log. + +The ``'live'`` attribute allows to trigger the actual creation of the device +once it's fully configured. The accepted values are: ``'1'`` to enable the +virtual device and ``'0'`` to disable and tear it down. + +Creating GPIO lookup tables +--------------------------- + +User can create a number of configfs groups under the device group with the +following properties: + +**Group:** ``/config/gpio-consumer/consumer-device/lookupX`` + +**Attribute:** ``/config/gpio-consumer/consumer-device/lookupX/key`` + +**Attribute:** ``/config/gpio-consumer/consumer-device/lookupX/offset`` + +**Attribute:** ``/config/gpio-consumer/consumer-device/lookupX/drive`` + +**Attribute:** ``/config/gpio-consumer/consumer-device/lookupX/pull`` + +**Attribute:** ``/config/gpio-consumer/consumer-device/lookupX/active_low`` + +**Attribute:** ``/config/gpio-consumer/consumer-device/lookupX/transitory`` + +This group represents a single entry in the GPIO lookup table. The name of the +sub-directory maps to the ``'con_id'`` field of ``'struct gpiod_lookup'``. The +``'key'`` attribute represents either the name of the chip this GPIO belongs to +or the GPIO line name. This depends on the value of the ``'offset'`` attribute: +if its value is >= 0, then ``'key'`` represents the label of the chip to lookup +while ``'offset'`` represents the offset of the line in that chip. If +``'offset'`` is < 0, then ``'key'`` represents the name of the line. + +The remaining attributes map to the ``'flags'`` field of the GPIO lookup struct. +The first two take string values as arguments: + +**``'drive'``:** ``'push-pull'``, ``'open-drain'``, ``'open-source'`` +**``'pull'``:** ``'pull-up'``, ``'pull-down'``, ``'pull-disabled'``, ``'as-is'`` + +``'active_low'`` and ``'transitory'`` are boolean attributes. + +Activating GPIO consumers +------------------------- + +Once the confiuration is complete, the ``'live'`` attribute must be set to 1 in +order to instantiate the consumer. It can be set back to 0 to destroy the +virtual devices. The module will synchronously wait for the new simulated device +to be successfully probed and if this doesn't happen, writing to ``'live'`` will +result in an error. + +Device-tree +----------- + +Virtual GPIO consumers can also be defined in device-tree. The compatible string +must be: ``"gpio-virtual-consumer"``. Supported properties are: + + ``"gpio-virtual-consumer,function"`` - function of the virtual device + ``"gpio-virtual-consumer,lines"`` - list of lookup keys + +An example device-tree code defining a virtual GPIO consumer: + +.. code-block :: none + + gpio-virt-consumer { + compatible = "gpio-virtual-consumer"; + + foo-gpios = <&gpio0 5 GPIO_ACTIVE_LOW>; + bar-gpios = <&gpio0 6 0>; + + gpio-virtual-consumer,function = "toggle"; + gpio-virtual-consumer,lines = "foo", "bar"; + }; + +.. SPDX-License-Identifier: GPL-2.0-or-later
drivers/gpio/Kconfig +7 -0
@@ -1762,6 +1762,13 @@ config GPIO_AGGREGATOR industrial control context, to be operated from userspace using the GPIO chardev interface. +config GPIO_CONSUMER + tristate "GPIO Consumer Testing Module" + select CONFIGFS_FS + help + This enables the configurable, configfs-based virtual GPIO consumer + testing driver. + config GPIO_LATCH tristate "GPIO latch driver" help
drivers/gpio/Makefile +1 -0
@@ -46,6 +46,7 @@ obj-$(CONFIG_GPIO_BT8XX) += gpio-bt8xx.o obj-$(CONFIG_GPIO_CADENCE) += gpio-cadence.o obj-$(CONFIG_GPIO_CLPS711X) += gpio-clps711x.o obj-$(CONFIG_GPIO_SNPS_CREG) += gpio-creg-snps.o +obj-$(CONFIG_GPIO_CONSUMER) += gpio-consumer.o obj-$(CONFIG_GPIO_CRYSTAL_COVE) += gpio-crystalcove.o obj-$(CONFIG_GPIO_CS5535) += gpio-cs5535.o obj-$(CONFIG_GPIO_DA9052) += gpio-da9052.o
drivers/gpio/gpio-consumer.c new file +954 -0
954 lines not on this page · raw message
Signature · 1 line
2.39.2
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Bartosz,
kernel test robot noticed the following build warnings:
[auto build test WARNING on brgl/gpio/for-next]
[also build test WARNING on linus/master v6.5-rc6 next-20230809] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Bartosz-Golaszewski/gpio-consumer-new-virtual-driver/20230813-030518 base: https://git.kernel.org/pub/scm/linux/kernel/git/brgl/linux.git gpio/for-next patch link: https://lore.kernel.org/r/20230812190359.5875-1-brgl%40bgdev.pl patch subject: [PATCH v4] gpio: consumer: new virtual driver reproduce: (https://download.01.org/0day-ci/archive/20230815/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> Documentation/admin-guide/gpio/gpio-consumer.rst: WARNING: document isn't included in any toctree
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki