Skip to contentSkip to conversation

Introduce block device LED trigger

PATCH Superseded Ian Pilcher 3 participants 6 messages Sep 14, 2021 – Sep 15, 2021

Subsystems (3)

v3 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v3 cover Introduce block device LED trigger

Changes from v2:
================

* Allow LEDs to be "linked" to partitions, as well as whole devices. Internally, the trigger now works with block_device structs, rather than gendisk structs.

(Investigating the lifecycle of block_device structs led me to discover the device resource API, so ...)

* Use the device resource API to manage the trigger's per-block device data structure (struct led_bdev_bdi). The trigger now uses a release function to remove references to block devices that have been removed.

Because the release function is automatically called by the driver core, there is no longer any need for the block layer to explictly call the trigger's cleanup function.

* Since there is no need to provide a built-in "stub" cleanup function when the trigger is built as a module, I have removed the always built-in "core" portion of the trigger.

* Without a built-in component, the module does need access to the block_class symbol. The second patch in this series exports the symbol to the LEDTRIG_BLKDEV namespace and explains the reason for doing so.

* Changed the interval sysfs attribute from a device attribute to a class attribute. It's single value that applies to all LEDs, so it didn't make sense as a device atribute.

* As requested, I am posting the trigger code (ledtrig-blkdev.c) as a single patch. This eliminates the commit messages that would otherwise describe sections of the code, so I have added fairly extensive comments to each function.

Changes from v1:
================

* Use correct address for LKML.

* Renamed the sysfs attributes used to manage and view the set of block devices associated ("linked") with an LED.

- /sys/class/leds/<LED>/link_device to create associations

- /sys/class/leds/<LED>/unlink_device to remove associations

- /sys/class/leds/<LED>/linked_devices/ contains symlinks to all block devices associated with the LED

- /sys/block/<DEVICE>/linked_leds (which only exists when the device is associated with at least one LED) contains symlinks to all LEDs with which the device is associated

link_device and unlink_device are write-only attributes, each of which represents a single action, rather than any state. (The current state is shown by the symbolic links in the <LED>/linked_devices/ and <DEVICE>/linked_leds/ directories.)

* Simplified sysfs attribute store functions. link_device and unlink_device no longer accept multiple devices at once, but this was really just an artifact of the way that sysfs repeatedly calls the store function when it doesn't "consume" all of its input, and it seemed to be confusing and unpopular anyway.

* Use DEVICE_ATTR_* macros (rather than __ATTR) for the sysfs attributes.

* Removed all pr_info() "system administrator error" messages.

* Different minimum values for LED blink time (10 ms) and activity check interval (25 ms).

v1 summary:
===========

This patch series adds a new "blkdev" LED trigger for disk (or other block device) activity LEDs.

It has the following functionality.

* Supports all types of block devices, including virtual devices (unlike the existing disk trigger which only works with ATA devices).

* LEDs can be configured to show read activity, write activity, or both.

* Supports multiple devices and multiple LEDs in arbitrary many-to-many configurations. For example, it is possible to configure multiple devices with device-specific read activity LEDs and a shared write activity LED. (See Documentation/leds/ledtrig-blkdev.rst in the first patch.)

* Doesn't add any overhead in the I/O path. Like the netdev LED trigger, it periodically checks the configured devices for activity and blinks its LEDs as appropriate.

* Blink duration (per LED) and interval between activity checks (global) are configurable.

* Requires minimal changes to the block subsystem.

- Adds 1 pointer to struct gendisk,

- Adds (inline function) call in device_add_disk() to ensure that the pointer is initialized to NULL (as protection against any drivers that allocate a gendisk themselves and don't use kzalloc()), and

- Adds call in del_gendisk() to remove a device from the trigger when that device is being removed.

These changes are all in patch #4, "block: Add block device LED trigger integrations."

* The trigger can be mostly built as a module.

When the trigger is modular, a small portion is built in to provide a "stub" function which can be called from del_gendisk(). The stub calls into the modular code via a function pointer when needed. The trigger also needs the ability to find gendisk's by name, which requires access to the un-exported block_class and disk_type symbols.

Ian Pilcher (3):
docs: Add block device (blkdev) LED trigger documentation
block: export block_class for use by the blkdev LED trigger
leds: trigger: Add block device LED trigger

 Documentation/ABI/testing/sysfs-block         |   9 +
 .../testing/sysfs-class-led-trigger-blkdev    |  43 +
 Documentation/leds/index.rst                  |   1 +
 Documentation/leds/ledtrig-blkdev.rst         | 144 +++
 block/genhd.c                                 |   1 +
 drivers/leds/trigger/Kconfig                  |   9 +
 drivers/leds/trigger/Makefile                 |   1 +
 drivers/leds/trigger/ledtrig-blkdev.c         | 889 ++++++++++++++++++
 8 files changed, 1097 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-led-trigger-blkdev
 create mode 100644 Documentation/leds/ledtrig-blkdev.rst
 create mode 100644 drivers/leds/trigger/ledtrig-blkdev.c

base-commit: 1619b69edce14c4a4665fa8ff4c587dcc77202a9

Signature · 1 line
2.31.1
#1 lore raw

PATCH v3 1/3 docs: Add block device (blkdev) LED trigger documentation

Add Documentation/ABI/testing/sysfs-class-led-trigger-blkdev to document:

* /sys/class/leds/<led>/blink_time
* /sys/class/leds/<led>/mode
* /sys/class/leds/<led>/link_device
* /sys/class/leds/<led>/unlink_device
* /sys/class/leds/<led>/linked_devices
* /sys/class/ledtrig_blkdev/interval

Add /sys/block/<disk>/linked_leds to
Documentation/ABI/testing/sysfs-block.

Add overview in Documentation/leds/ledtrig-blkdev.rst.

---
 Documentation/ABI/testing/sysfs-block         |   9 ++
 .../testing/sysfs-class-led-trigger-blkdev    |  43 ++++++
 Documentation/leds/index.rst                  |   1 +
 Documentation/leds/ledtrig-blkdev.rst         | 144 ++++++++++++++++++
 4 files changed, 197 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-led-trigger-blkdev
 create mode 100644 Documentation/leds/ledtrig-blkdev.rst
Documentation/ABI/testing/sysfs-block +9 -0
index a0ed87386639..80d4becc4e6d 100644
@@ -328,3 +328,12 @@ Description:
 		does not complete in this time then the block driver timeout
 		handler is invoked. That timeout handler can decide to retry
 		the request, to fail it or to start a device recovery strategy.
+
+What:		/sys/block/<disk>/linked_leds
+Date:		September 2021
+Contact:	Ian Pilcher <[email protected]>
+Description:
+		Directory containing links to all LEDs that are associated
+		with this block device through the blkdev LED trigger.  Only
+		present when at least one LED is associated.  (See
+		Documentation/leds/ledtrig-blkdev.rst.)
Documentation/ABI/testing/sysfs-class-led-trigger-blkdev new file +43 -0

43 lines not on this page · raw message

Documentation/leds/index.rst +1 -0
index e5d63b940045..e3c24e468cbc 100644
@@ -10,6 +10,7 @@ LEDs
    leds-class
    leds-class-flash
    leds-class-multicolor
+   ledtrig-blkdev
    ledtrig-oneshot
    ledtrig-transient
    ledtrig-usbport
Documentation/leds/ledtrig-blkdev.rst new file +144 -0

144 lines not on this page · raw message

Signature · 1 line
2.31.1
#2 lore raw

PATCH v3 2/3 block: export block_class for use by the blkdev LED trigger

In reply to Ian Pilcher

Export block_class to the LEDTRIG_BLKDEV namespace.

The block device (blkdev) LED trigger periodically checks the activity counters of block devices and blinks LEDs to reflect that activity.

The specific block device(s) checked can be configured through a sysfs API. Once an LED has been associated with the blkdev trigger, the LED can be "linked" to a block device by writing that device's name to the LED's link_device attribute. For example:

echo sda > /sys/class/leds/<led>/link_device

When a block device name is written to the sysfs attribute, the trigger uses class_find_device_by_name() to resolve the actual block_device structure:

  dev = class_find_device_by_name(&block_class, name);

Exporting the block_class symbol makes it possible to build the LED trigger as a module.

---
 block/genhd.c | 1 +
 1 file changed, 1 insertion(+)
block/genhd.c +1 -0
index 7b6e5e1cf956..ebea496bc283 100644
@@ -1076,6 +1076,7 @@ struct class block_class = {
 	.name		= "block",
 	.dev_uevent	= block_uevent,
 };
+EXPORT_SYMBOL_NS_GPL(block_class, LEDTRIG_BLKDEV);
 
 static char *block_devnode(struct device *dev, umode_t *mode,
 			   kuid_t *uid, kgid_t *gid)
Signature · 1 line
2.31.1
#3 lore raw

Re: [PATCH v3 2/3] block: export block_class for use by the blkdev LED trigger

NAK. block_class has no business being used outside the core block layer.

#4 lore raw

PATCH v3 3/3 leds: trigger: Add block device LED trigger

In reply to Ian Pilcher

Add LEDS_TRIGGER_BLKDEV (tristate) config option to control building of the trigger.

---
 drivers/leds/trigger/Kconfig          |   9 +
 drivers/leds/trigger/Makefile         |   1 +
 drivers/leds/trigger/ledtrig-blkdev.c | 889 ++++++++++++++++++++++++++
 3 files changed, 899 insertions(+)
 create mode 100644 drivers/leds/trigger/ledtrig-blkdev.c
drivers/leds/trigger/Kconfig +9 -0
index 1f1d57288085..219d15c046d7 100644
@@ -153,4 +153,13 @@ config LEDS_TRIGGER_TTY
 
 	  When build as a module this driver will be called ledtrig-tty.
 
+config LEDS_TRIGGER_BLKDEV
+	tristate "LED Trigger for block devices"
+	depends on BLOCK
+	help
+	  The blkdev LED trigger allows LEDs to be controlled by block device
+	  activity (reads and writes).
+
+	  See Documentation/leds/ledtrig-blkdev.rst.
+
 endif # LEDS_TRIGGERS
drivers/leds/trigger/Makefile +1 -0
index 25c4db97cdd4..d53bab5d93f1 100644
@@ -16,3 +16,4 @@ obj-$(CONFIG_LEDS_TRIGGER_NETDEV)	+= ledtrig-netdev.o
 obj-$(CONFIG_LEDS_TRIGGER_PATTERN)	+= ledtrig-pattern.o
 obj-$(CONFIG_LEDS_TRIGGER_AUDIO)	+= ledtrig-audio.o
 obj-$(CONFIG_LEDS_TRIGGER_TTY)		+= ledtrig-tty.o
+obj-$(CONFIG_LEDS_TRIGGER_BLKDEV)	+= ledtrig-blkdev.o
drivers/leds/trigger/ledtrig-blkdev.c new file +889 -0

889 lines not on this page · raw message

Signature · 1 line
2.31.1
#5 lore raw

Re: [PATCH v3 3/3] leds: trigger: Add block device LED trigger

Quoted 10 lines · Ian Pilcher

Add LEDS_TRIGGER_BLKDEV (tristate) config option to control building of the trigger.

---
 drivers/leds/trigger/Kconfig          |   9 +
 drivers/leds/trigger/Makefile         |   1 +
 drivers/leds/trigger/ledtrig-blkdev.c | 889 ++++++++++++++++++++++++++
 3 files changed, 899 insertions(+)
 create mode 100644 drivers/leds/trigger/ledtrig-blkdev.c

This change does a lot more than what you described in the changelog text above :(