gpio-sim: configfs-based GPIO simulator
Subsystems (4)
v9 cover gpio-sim: configfs-based GPIO simulator
This is another shot at the gpio-sim testing module. As there was no reasoning with configfs maintainers for many months, this time the whole concept of committable items has been dropped. Instead, each configfs chip item (or rather a group - more on that later) exposes a new attribute called 'live'. Writing 1 to it brings the chip on-line (registers the platform device) and writing 0 tears it down.
There are some caveats to that approach - for example: we can't block the user-space from deleting chip items when chips are live but is just handled by silently destroying the chip device in the background.
Andy (rightfully) pointed out that parsing of the lists of line names is awkward so in this iteration it's been replaced by a system that is more elegant and will allow to easily extend configuration options for specific GPIO lines. This is achieved by turning the chip's configfs item into a configfs group and allowing the user-space to create additional items inside it. The items must be called line<offset> (e.g. line0, line12 etc.) where the offset part indicates to the module the offset for which given item stores the configuration for. Within each such line item, there are additional attributes that allow specifying configuration for specific lines. Currently we only support the 'name' attribute but I plan to extend that to support GPIO hogging too.
v1 -> v2:
- add selftests for gpio-sim
- add helper programs for selftests
- update the configfs rename callback to work with the new API introduced in v5.11
- fix a missing quote in the documentation
- use !! whenever using bits operation that are required to return 0 or 1
- use provided bitmap API instead of reimplementing copy or fill operations
- fix a deadlock in gpio_sim_direction_output()
- add new read-only configfs attributes for mapping of configfs items to GPIO device names
- and address other minor issues pointed out in reviews of v1
v2 -> v3:
- use devm_bitmap_alloc() instead of the zalloc variant if we're initializing the bitmap with 1s
- drop the patch exporting device_is_bound()
- don't return -ENODEV from dev_nam and chip_name configfs attributes, return a string indicating that the device is not available yet ('n/a')
- fix indentation where it makes sense
- don't protect IDA functions which use their own locking and where it's not needed
- use kmemdup() instead of kzalloc() + memcpy()
- collected review tags
- minor coding style fixes
v3 -> v4:
- return 'none' instead of 'n/a' from dev_name and chip_name before the device is registered
- use sysfs_emit() instead of s*printf()
- drop GPIO_SIM_MAX_PROP as it's only used in an array's definition where it's fine to hardcode the value
v4 -> v5:
- drop lib patches that are already upstream
- use BIT() instead of (1UL << bit) for flags
- fix refcounting for the configfs_dirent in rename()
- drop d_move() from the rename() callback
- free memory allocated for the live and pending groups in configfs_d_iput() and not in detach_groups()
- make sure that if a group of some name is in the live directory, a new group with the same name cannot be created in the pending directory
v5 -> v6:
- go back to using (1UL << bit) instead of BIT()
- if the live group dentry doesn't exist for whatever reason at the time when mkdir() in the pending group is called (would be a BUG()), return -ENOENT instead of -EEXIST which should only be returned if given subsystem already exists in either live or pending group
v6 -> v7:
- as detailed by Andy in commit 6fda593f3082 ("gpio: mockup: Convert to use software nodes") removing device properties after the platform device is removed but before the GPIO device gets dropped can lead to a use-after-free bug - use software nodes to manually control the freeing of the properties
v7 -> v8:
- fixed some minor coding style issues as pointed out by Andy
v8 -> v9:
- dropped the patches implementing committable-items and reworked the driver to not use them
- reworked the gpio-line-names property and configuring specific lines in general
- many smaller tweaks here and there
Bartosz Golaszewski (4):
gpio: sim: new testing module
selftests: gpio: provide a helper for reading chip info
selftests: gpio: add a helper for reading GPIO line names
selftests: gpio: add test cases for gpio-sim
Documentation/admin-guide/gpio/gpio-sim.rst | 67 ++ drivers/gpio/Kconfig | 8 + drivers/gpio/Makefile | 1 + drivers/gpio/gpio-sim.c | 990 ++++++++++++++++++ tools/testing/selftests/gpio/.gitignore | 2 + tools/testing/selftests/gpio/Makefile | 4 +- tools/testing/selftests/gpio/config | 1 + tools/testing/selftests/gpio/gpio-chip-info.c | 57 + tools/testing/selftests/gpio/gpio-line-name.c | 55 + tools/testing/selftests/gpio/gpio-sim.sh | 266 +++++ 10 files changed, 1449 insertions(+), 2 deletions(-) create mode 100644 Documentation/admin-guide/gpio/gpio-sim.rst create mode 100644 drivers/gpio/gpio-sim.c create mode 100644 tools/testing/selftests/gpio/gpio-chip-info.c create mode 100644 tools/testing/selftests/gpio/gpio-line-name.c create mode 100755 tools/testing/selftests/gpio/gpio-sim.sh
Signature · 1 line
2.25.1
PATCH v9 1/4 gpio: sim: new testing module
Implement a new, modern GPIO testing module controlled by configfs attributes instead of module parameters. The goal of this driver is to provide a replacement for gpio-mockup that will be easily extensible with new features and doesn't require reloading the module to change the setup.
--- Documentation/admin-guide/gpio/gpio-sim.rst | 67 ++ drivers/gpio/Kconfig | 8 + drivers/gpio/Makefile | 1 + drivers/gpio/gpio-sim.c | 990 ++++++++++++++++++++ 4 files changed, 1066 insertions(+) create mode 100644 Documentation/admin-guide/gpio/gpio-sim.rst create mode 100644 drivers/gpio/gpio-sim.c
Documentation/admin-guide/gpio/gpio-sim.rst new file +67 -0
@@ -0,0 +1,67 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +Configfs GPIO Simulator +======================= + +The configfs GPIO Simulator (gpio-sim) provides a way to create simulated GPIO +chips for testing purposes. The lines exposed by these chips can be accessed +using the standard GPIO character device interface as well as manipulated +using sysfs attributes. + +Creating simulated chips +------------------------ + +The gpio-sim module registers a configfs subsystem called 'gpio-sim'. + +In order to instantiate a new simulated chip, the user needs to mkdir() a new +directory gpio-sim/. Inside each new directory, there's a set of attributes +that can be used to configure the new chip. Additionally the user can mkdir() +subdirectories inside the chip's directory that allow to pass additional +configuration for specific lines. The name of those subdirectories must take +the form of: 'line<offset>' (e.g. 'line0', 'line20', etc.) as the name will be +used by the module to assign the config to the specific line at given offset. + +Once the confiuration is complete, the 'active' attribute must be set to 1 in +order to instantiate the chip. It can be set back to 0 to destroy the simulated +chip. + +Currently supported chip configuration attributes are: + + num_lines - an unsigned integer value defining the number of GPIO lines to + export + + label - a string defining the label for the GPIO chip + +Additionally two read-only attributes named 'chip_name' and 'dev_name' are +exposed in order to provide users with a mapping from configfs directories to +the actual devices created in the kernel. The former returns the name of the +GPIO device as assigned by gpiolib (i.e. "gpiochip0", "gpiochip1", etc.). The +latter returns the parent device name as defined by the gpio-sim driver (i.e. +"gpio-sim.0", "gpio-sim.1", etc.). This allows user-space to map the configfs +items both to the correct character device file as well as the associated entry +in sysfs. + +Supported line configuration attributes are: + + name - a string defining the name of this line as used by the + "gpio-line-names" device property + +Simulated GPIO chips can also be defined in device-tree. The compatible string +must be: "gpio-simulator". Supported properties are: + + "gpio-sim,label" - chip label + +Other standard GPIO properties (like "gpio-line-names", "ngpios" or gpio-hog) +are also supported. + +Manipulating simulated lines +---------------------------- + +Each simulated GPIO chip creates a sysfs attribute group under its device +directory called 'line-ctrl'. Inside each group, there's a separate attribute +for each GPIO line. The name of the attribute is of the form 'gpioX' where X +is the line's offset in the chip. + +Reading from a line attribute returns the current value. Writing to it (0 or 1) +changes the configuration of the simulated pull-up/pull-down resistor +(1 - pull-up, 0 - pull-down).
drivers/gpio/Kconfig +8 -0
@@ -1693,6 +1693,14 @@ config GPIO_VIRTIO These virtual GPIOs can be routed to real GPIOs or attached to simulators on the host (like QEMU). +config GPIO_SIM + tristate "GPIO Simulator Module" + select IRQ_SIM + select CONFIGFS_FS + help + This enables the GPIO simulator - a configfs-based GPIO testing + driver. + endmenu endif
drivers/gpio/Makefile +1 -0
@@ -133,6 +133,7 @@ obj-$(CONFIG_GPIO_SAMA5D2_PIOBU) += gpio-sama5d2-piobu.o obj-$(CONFIG_GPIO_SCH311X) += gpio-sch311x.o obj-$(CONFIG_GPIO_SCH) += gpio-sch.o obj-$(CONFIG_GPIO_SIFIVE) += gpio-sifive.o +obj-$(CONFIG_GPIO_SIM) += gpio-sim.o obj-$(CONFIG_GPIO_SIOX) += gpio-siox.o obj-$(CONFIG_GPIO_SL28CPLD) += gpio-sl28cpld.o obj-$(CONFIG_GPIO_SODAVILLE) += gpio-sodaville.o
drivers/gpio/gpio-sim.c new file +990 -0
990 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v9 1/4] gpio: sim: new testing module
Quoted 7 lines · Bartosz Golaszewski
Implement a new, modern GPIO testing module controlled by configfs attributes instead of module parameters. The goal of this driver is to provide a replacement for gpio-mockup that will be easily extensible with new features and doesn't require reloading the module to change the setup.
Signed-off-by Bartosz Golaszewski <[email protected]>
The series:
Yours,
Linus Walleij
PATCH v9 2/4 selftests: gpio: provide a helper for reading chip info
In reply to Bartosz GolaszewskiAdd a simple program that allows to retrieve chip properties from the GPIO character device. This will be used in gpio-sim selftests.
--- tools/testing/selftests/gpio/.gitignore | 1 + tools/testing/selftests/gpio/Makefile | 2 +- tools/testing/selftests/gpio/gpio-chip-info.c | 57 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/gpio/gpio-chip-info.c
tools/testing/selftests/gpio/.gitignore +1 -0
@@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only gpio-mockup-cdev +gpio-chip-info
tools/testing/selftests/gpio/Makefile +1 -1
@@ -2,6 +2,6 @@ TEST_PROGS := gpio-mockup.sh TEST_FILES := gpio-mockup-sysfs.sh -TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev +TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info include ../lib.mk
tools/testing/selftests/gpio/gpio-chip-info.c new file +57 -0
57 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v9 3/4 selftests: gpio: add a helper for reading GPIO line names
In reply to Bartosz GolaszewskiAdd a simple program that allows to read GPIO line names from the character device. This will be used in gpio-sim selftests.
--- tools/testing/selftests/gpio/.gitignore | 1 + tools/testing/selftests/gpio/Makefile | 2 +- tools/testing/selftests/gpio/gpio-line-name.c | 55 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/gpio/gpio-line-name.c
tools/testing/selftests/gpio/.gitignore +1 -0
@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only gpio-mockup-cdev gpio-chip-info +gpio-line-name
tools/testing/selftests/gpio/Makefile +1 -1
@@ -2,6 +2,6 @@ TEST_PROGS := gpio-mockup.sh TEST_FILES := gpio-mockup-sysfs.sh -TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info +TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info gpio-line-name include ../lib.mk
tools/testing/selftests/gpio/gpio-line-name.c new file +55 -0
55 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v9 4/4 selftests: gpio: add test cases for gpio-sim
In reply to Bartosz GolaszewskiAdd a set of tests for the new gpio-sim module. This is a pure shell test-suite and uses the helper programs available in the gpio selftests directory. These test-cases only test the functionalities exposed by the gpio-sim driver, not those handled by core gpiolib code.
--- tools/testing/selftests/gpio/Makefile | 2 +- tools/testing/selftests/gpio/config | 1 + tools/testing/selftests/gpio/gpio-sim.sh | 266 +++++++++++++++++++++++ 3 files changed, 268 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/gpio/gpio-sim.sh
tools/testing/selftests/gpio/Makefile +1 -1
@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 -TEST_PROGS := gpio-mockup.sh +TEST_PROGS := gpio-mockup.sh gpio-sim.sh TEST_FILES := gpio-mockup-sysfs.sh TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info gpio-line-name
tools/testing/selftests/gpio/config +1 -0
@@ -1,3 +1,4 @@ CONFIG_GPIOLIB=y CONFIG_GPIO_CDEV=y CONFIG_GPIO_MOCKUP=m +CONFIG_GPIO_SIM=m
tools/testing/selftests/gpio/gpio-sim.sh new file +266 -0
266 lines not on this page · raw message
Signature · 1 line
2.25.1
Quoted 11 lines · Bartosz Golaszewski
This is another shot at the gpio-sim testing module. As there was no reasoning with configfs maintainers for many months, this time the whole concept of committable items has been dropped. Instead, each configfs chip item (or rather a group - more on that later) exposes a new attribute called 'live'. Writing 1 to it brings the chip on-line (registers the platform device) and writing 0 tears it down.
There are some caveats to that approach - for example: we can't block the user-space from deleting chip items when chips are live but is just handled by silently destroying the chip device in the background.
Quoted 11 lines
Andy (rightfully) pointed out that parsing of the lists of line names is awkward so in this iteration it's been replaced by a system that is more elegant and will allow to easily extend configuration options for specific GPIO lines. This is achieved by turning the chip's configfs item into a configfs group and allowing the user-space to create additional items inside it. The items must be called line<offset> (e.g. line0, line12 etc.) where the offset part indicates to the module the offset for which given item stores the configuration for. Within each such line item, there are additional attributes that allow specifying configuration for specific lines. Currently we only support the 'name' attribute but I plan to extend that to support GPIO hogging too.
One question here. Since you know how the driver looks like in both cases (with and without committable items), would it be possible to modify what you proposed here to the former one in case ConfigFS gains the feature?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 25 lines · Andy Shevchenko
Quoted 11 lines · Bartosz Golaszewski
This is another shot at the gpio-sim testing module. As there was no reasoning with configfs maintainers for many months, this time the whole concept of committable items has been dropped. Instead, each configfs chip item (or rather a group - more on that later) exposes a new attribute called 'live'. Writing 1 to it brings the chip on-line (registers the platform device) and writing 0 tears it down.
There are some caveats to that approach - for example: we can't block the user-space from deleting chip items when chips are live but is just handled by silently destroying the chip device in the background.
Quoted 11 lines
Andy (rightfully) pointed out that parsing of the lists of line names is awkward so in this iteration it's been replaced by a system that is more elegant and will allow to easily extend configuration options for specific GPIO lines. This is achieved by turning the chip's configfs item into a configfs group and allowing the user-space to create additional items inside it. The items must be called line<offset> (e.g. line0, line12 etc.) where the offset part indicates to the module the offset for which given item stores the configuration for. Within each such line item, there are additional attributes that allow specifying configuration for specific lines. Currently we only support the 'name' attribute but I plan to extend that to support GPIO hogging too.
Quoted 4 lines
One question here. Since you know how the driver looks like in both cases (with and without committable items), would it be possible to modify what you proposed here to the former one in case ConfigFS gains the feature?
This would completely change the user interface unfortunately. We could extend it but we would need to keep this one too most likely.
TBH I don't see the committable items merged anytime soon, and this is GoodEnough®.
Bart
Quoted 31 lines · Bartosz Golaszewski
Quoted 25 lines · Andy Shevchenko
Quoted 11 lines · Bartosz Golaszewski
This is another shot at the gpio-sim testing module. As there was no reasoning with configfs maintainers for many months, this time the whole concept of committable items has been dropped. Instead, each configfs chip item (or rather a group - more on that later) exposes a new attribute called 'live'. Writing 1 to it brings the chip on-line (registers the platform device) and writing 0 tears it down.
There are some caveats to that approach - for example: we can't block the user-space from deleting chip items when chips are live but is just handled by silently destroying the chip device in the background.
Quoted 11 lines
Andy (rightfully) pointed out that parsing of the lists of line names is awkward so in this iteration it's been replaced by a system that is more elegant and will allow to easily extend configuration options for specific GPIO lines. This is achieved by turning the chip's configfs item into a configfs group and allowing the user-space to create additional items inside it. The items must be called line<offset> (e.g. line0, line12 etc.) where the offset part indicates to the module the offset for which given item stores the configuration for. Within each such line item, there are additional attributes that allow specifying configuration for specific lines. Currently we only support the 'name' attribute but I plan to extend that to support GPIO hogging too.
Quoted 3 lines
One question here. Since you know how the driver looks like in both cases (with and without committable items), would it be possible to modify what you proposed here to the former one in case ConfigFS gains the feature?
Quoted 5 lines
This would completely change the user interface unfortunately. We could extend it but we would need to keep this one too most likely.
TBH I don't see the committable items merged anytime soon, and this is GoodEnough®.
Fine with me then!
Thanks for doing this all, I know it's a bit delayed in terms of getting into upstream.
Btw, gpio-mockup testing scripts have an issue that the number of lines to check overflow is hardcoded and since x86_64 switched to 1024 from 512 it reveals the issue. Does gpio-sim solve this in a better way (like telling to user space the ngpios, etc)?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 41 lines · Andy Shevchenko
Quoted 31 lines · Bartosz Golaszewski
Quoted 25 lines · Andy Shevchenko
Quoted 11 lines · Bartosz Golaszewski
This is another shot at the gpio-sim testing module. As there was no reasoning with configfs maintainers for many months, this time the whole concept of committable items has been dropped. Instead, each configfs chip item (or rather a group - more on that later) exposes a new attribute called 'live'. Writing 1 to it brings the chip on-line (registers the platform device) and writing 0 tears it down.
There are some caveats to that approach - for example: we can't block the user-space from deleting chip items when chips are live but is just handled by silently destroying the chip device in the background.
Quoted 11 lines
Andy (rightfully) pointed out that parsing of the lists of line names is awkward so in this iteration it's been replaced by a system that is more elegant and will allow to easily extend configuration options for specific GPIO lines. This is achieved by turning the chip's configfs item into a configfs group and allowing the user-space to create additional items inside it. The items must be called line<offset> (e.g. line0, line12 etc.) where the offset part indicates to the module the offset for which given item stores the configuration for. Within each such line item, there are additional attributes that allow specifying configuration for specific lines. Currently we only support the 'name' attribute but I plan to extend that to support GPIO hogging too.
Quoted 3 lines
One question here. Since you know how the driver looks like in both cases (with and without committable items), would it be possible to modify what you proposed here to the former one in case ConfigFS gains the feature?
Quoted 5 lines
This would completely change the user interface unfortunately. We could extend it but we would need to keep this one too most likely.
TBH I don't see the committable items merged anytime soon, and this is GoodEnough®.
Fine with me then!
Quoted 8 lines
Thanks for doing this all, I know it's a bit delayed in terms of getting into upstream.
Btw, gpio-mockup testing scripts have an issue that the number of lines to check overflow is hardcoded and since
x86_64switched to 1024 from 512 it reveals the issue. Does gpio-sim solve this in a better way (like telling to user space the ngpios, etc)?
Yeah the selftests need fixing now.
No, there's no fix for that in gpio-sim - probe() will just fail. Which makes me think - maybe we should synchronously wait when writing to 'live' for the probe to return (for instance setup a notifier) so that we know if the chip probed correctly. Then we can notify the user-space about the error destroy the device too.
Bart
Quoted 46 lines · Bartosz Golaszewski
Quoted 44 lines · Andy Shevchenko
Quoted 31 lines · Bartosz Golaszewski
Quoted 25 lines · Andy Shevchenko
Quoted 11 lines · Bartosz Golaszewski
This is another shot at the gpio-sim testing module. As there was no reasoning with configfs maintainers for many months, this time the whole concept of committable items has been dropped. Instead, each configfs chip item (or rather a group - more on that later) exposes a new attribute called 'live'. Writing 1 to it brings the chip on-line (registers the platform device) and writing 0 tears it down.
There are some caveats to that approach - for example: we can't block the user-space from deleting chip items when chips are live but is just handled by silently destroying the chip device in the background.
Quoted 11 lines
Andy (rightfully) pointed out that parsing of the lists of line names is awkward so in this iteration it's been replaced by a system that is more elegant and will allow to easily extend configuration options for specific GPIO lines. This is achieved by turning the chip's configfs item into a configfs group and allowing the user-space to create additional items inside it. The items must be called line<offset> (e.g. line0, line12 etc.) where the offset part indicates to the module the offset for which given item stores the configuration for. Within each such line item, there are additional attributes that allow specifying configuration for specific lines. Currently we only support the 'name' attribute but I plan to extend that to support GPIO hogging too.
Quoted 3 lines
One question here. Since you know how the driver looks like in both cases (with and without committable items), would it be possible to modify what you proposed here to the former one in case ConfigFS gains the feature?
Quoted 5 lines
This would completely change the user interface unfortunately. We could extend it but we would need to keep this one too most likely.
TBH I don't see the committable items merged anytime soon, and this is GoodEnough®.
Fine with me then!
Thanks for doing this all, I know it's a bit delayed in terms of getting into upstream.
Quoted 8 lines
Quoted 5 lines
Btw, gpio-mockup testing scripts have an issue that the number of lines to check overflow is hardcoded and since
x86_64switched to 1024 from 512 it reveals the issue. Does gpio-sim solve this in a better way (like telling to user space the ngpios, etc)?Yeah the selftests need fixing now.
No, there need to be new selftests added for your gpio-sim. The existing selftests cover the gpio-mockup itself.
Quoted 6 lines
No, there's no fix for that in gpio-sim -
probe()will just fail. Which makes me think - maybe we should synchronously wait when writing to 'live' for the probe to return (for instance setup a notifier) so that we know if the chip probed correctly. Then we can notify the user-space about the error destroy the device too.
+1 - need definite feedback to userspace that the change to the test setup is in place so tests can proceed. No arbitrary waits or waiting for events from other subsystems like udev as we have to do with gpio-mockup.
Cheers,
Kent.
Quoted 6 lines · Bartosz Golaszewski
v8 -> v9:
- dropped the patches implementing committable-items and reworked the driver to not use them
- reworked the gpio-line-names property and configuring specific lines in general
- many smaller tweaks here and there
The series:
Let's go with this.
Yours,
Linus Walleij
Quoted 10 lines · Linus Walleij
Quoted 6 lines · Bartosz Golaszewski
v8 -> v9:
- dropped the patches implementing committable-items and reworked the driver to not use them
- reworked the gpio-line-names property and configuring specific lines in general
- many smaller tweaks here and there
Quoted 6 lines
The series:
Acked-by Linus Walleij <[email protected]>Let's go with this.
Yours,
Linus Walleij
Thanks but Kent and Andy are right, I'm about to send another version that synchronously waits during echo 1 > live for the device to come on-line.
Bart