From patchwork Thu Jul 29 01:53:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Pilcher X-Patchwork-Id: 489031 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47C0AC4320E for ; Thu, 29 Jul 2021 01:54:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2EE1561040 for ; Thu, 29 Jul 2021 01:54:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233270AbhG2Byd (ORCPT ); Wed, 28 Jul 2021 21:54:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233234AbhG2Byc (ORCPT ); Wed, 28 Jul 2021 21:54:32 -0400 Received: from mail-ot1-x330.google.com (mail-ot1-x330.google.com [IPv6:2607:f8b0:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61418C061765; Wed, 28 Jul 2021 18:54:29 -0700 (PDT) Received: by mail-ot1-x330.google.com with SMTP id c7-20020a9d27870000b02904d360fbc71bso4155969otb.10; Wed, 28 Jul 2021 18:54:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pVpQK3T6/YXyYNLRmSxyloWODC3EEBXIYjhI53mzXu8=; b=JAstWXGl1hp6+T7Vbu6pjBo3duiubfGtoY6ipH+GiGphbKF8rBS6e+TmeviduQMTyA hrdu7vFuTWTeAgS5mGWrCHpt55ixLcxp957LoB6BuQVAFYVzQ2BrvLCDzf9+DF0rY8Bx Q1F2aw+5HpaaOX/MsbgmA/QoPpiLtenNeuZjkRh97ezHDeKNhRJ55H6QtY/Rlkzun39N JPIXnFvzuqOFAJmP9Y3pDlY5EQhmWoTwyHdBaLKF6cMp5jT2JnqcViZ+ZGNCnAFdCVBG PKflNnSjMupJsux6ZNKMQEm4qCNQ1GK2FThw3kPOClXH9JuaIzcMR9qfCLl1qb5jIMd+ Nttg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pVpQK3T6/YXyYNLRmSxyloWODC3EEBXIYjhI53mzXu8=; b=aGFtZlX6FLW/vlpvv6ITnFopVUUVv8FykoDpR3BqFsjlSVNlcLIAaXa9TFIIf6t1ws /Ug35d8rUyZQoxUP2d8ADlkbROnBeJvYN/g92mUMpvyTHk6NlzQrV9Oux+PPbOe76NyJ bzQTK1i6FckXOkxN4PHBJPXjbcB+aSOyUD3X+hBnizp1xsR5qVX1PpBBeYWbJ7NGO72U 5XsdpzPOTCpNnQESFgcpt5krmGwqRUMC5eF1G+VFddnHIvZcSEbN7xXLvGyP6FSPycHF buUPGAvcvrWuQG9HAEo0CHK/YNqeGJCZvanIOJqpQMU4ieqn7nrmooyvmdMKs4qOYpn/ V6kg== X-Gm-Message-State: AOAM531WBViNbFgFzUVY6FZinLB49agO2TrktR2WX0GbT/Mjuw3Omz0u /E3W7e39UkgKZgjRTYf9l4COIwdvjeUYb3xH X-Google-Smtp-Source: ABdhPJyU7Kdy/yISBb3spl8mSsIIKSuRVPfLyVyjE5VHkSqr5SeTSE4a3kI9iVe4Peko4n9aSrIm5g== X-Received: by 2002:a9d:6e06:: with SMTP id e6mr1925225otr.350.1627523668769; Wed, 28 Jul 2021 18:54:28 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id c11sm311424otm.37.2021.07.28.18.54.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 18:54:28 -0700 (PDT) From: Ian Pilcher To: linux-block@vger.kernel.org, linux-leds@vger.kernel.org Cc: axboe@kernel.dk, pavel@ucw.cz, linux-kernel@vger.kernel.org, kernelnewbies@kernelnewbies.org, Ian Pilcher Subject: [RFC PATCH 1/8] docs: Add block device LED trigger documentation Date: Wed, 28 Jul 2021 20:53:37 -0500 Message-Id: <20210729015344.3366750-2-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210729015344.3366750-1-arequipeno@gmail.com> References: <20210729015344.3366750-1-arequipeno@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org * Document the sysfs attributes (/sys/class/block/led_trigger_* and /sys/class/block/${DEV}/led_trigger) that can be used to create, list, and delete block device LED triggers and to set and clear device/trigger associations. * Pull API documentation from block/blk-ledtrig.c (once it exists). Signed-off-by: Ian Pilcher --- Documentation/block/index.rst | 1 + Documentation/block/led-triggers.rst | 124 +++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 Documentation/block/led-triggers.rst diff --git a/Documentation/block/index.rst b/Documentation/block/index.rst index 86dcf7159f99..a125ecdb4c7b 100644 --- a/Documentation/block/index.rst +++ b/Documentation/block/index.rst @@ -25,3 +25,4 @@ Block stat switching-sched writeback_cache_control + led-triggers diff --git a/Documentation/block/led-triggers.rst b/Documentation/block/led-triggers.rst new file mode 100644 index 000000000000..a67e06c68073 --- /dev/null +++ b/Documentation/block/led-triggers.rst @@ -0,0 +1,124 @@ +.. SPDX-License-Identifier: GPL-2.0 + +============ +LED Triggers +============ + +Available when ``CONFIG_BLK_LED_TRIGGERS=y``. + +sysfs interface +=============== + +Create a new block device LED trigger:: + + # echo foo > /sys/class/block/led_trigger_new + +The name must be unique among all LED triggers (not just block device LED +triggers). + +Create two more:: + + # echo bar baz > /sys/class/block/led_trigger_new + +List the triggers:: + + # cat /sys/class/block/led_trigger_list + baz: 0 + bar: 0 + foo: 0 + +(The number after each trigger is its reference count.) + +Associate a trigger with a block device:: + + # cat /sys/class/block/sda/led_trigger + (none) + + # echo foo > /sys/class/block/sda/led_trigger + # cat /sys/class/block/sda/led_trigger + foo + +Note that ``foo``'s reference count has increased, and it cannot be deleted:: + + # cat /sys/class/block/led_trigger_list + baz: 0 + bar: 0 + foo: 1 + + # echo foo > /sys/class/block/led_trigger_del + -bash: echo: write error: Device or resource busy + + # dmesg | tail -n 1 + [23176.475424] blockdev LED trigger foo still in use + +Associate the ``foo`` trigger with an LED:: + + # cat /sys/class/leds/input1::scrolllock/trigger + none usb-gadget usb-host rc-feedback [kbd-scrolllock] kbd-numlock + kbd-capslock kbd-kanalock kbd-shiftlock kbd-altgrlock kbd-ctrllock + kbd-altlock kbd-shiftllock kbd-shiftrlock kbd-ctrlllock kbd-ctrlrlock + disk-activity disk-read disk-write ide-disk mtd nand-disk panic + audio-mute audio-micmute rfkill-any rfkill-none foo bar baz + + # echo foo > /sys/class/leds/input1::scrolllock/trigger + + # cat /sys/class/leds/input1::scrolllock/trigger + none usb-gadget usb-host rc-feedback [kbd-scrolllock] kbd-numlock + kbd-capslock kbd-kanalock kbd-shiftlock kbd-altgrlock kbd-ctrllock + kbd-altlock kbd-shiftllock kbd-shiftrlock kbd-ctrlllock kbd-ctrlrlock + disk-activity disk-read disk-write ide-disk mtd nand-disk panic + audio-mute audio-micmute rfkill-any rfkill-none [foo] bar baz + +Reads and writes to ``sda`` should now cause the scroll lock LED on your +keyboard to blink (assuming that it has one). + +Multiple devices can be associated with a trigger:: + + # echo foo > /sys/class/block/sdb/led_trigger + + # cat /sys/class/block/led_trigger_list + baz: 0 + bar: 0 + foo: 2 + +Activity on either ``sda`` or ``sdb`` should now be shown by your scroll lock +LED. + +Clear ``sda``'s LED trigger:: + + # echo > /sys/class/block/sda/led_trigger + + # cat /sys/class/block/sda/led_trigger + (none) + + # cat /sys/class/block/led_trigger_list + baz: 0 + bar: 0 + foo: 1 + +And ``sdb``'s trigger:: + + # echo > /sys/class/block/sdb/led_trigger + +Delete the triggers:: + + # echo foo bar baz > /sys/class/block/led_trigger_del + + # cat /sys/class/block/led_trigger_list + + # cat /sys/class/leds/input1::scrolllock/trigger + none usb-gadget usb-host rc-feedback [kbd-scrolllock] kbd-numlock + kbd-capslock kbd-kanalock kbd-shiftlock kbd-altgrlock kbd-ctrllock + kbd-altlock kbd-shiftllock kbd-shiftrlock kbd-ctrlllock kbd-ctrlrlock + disk-activity disk-read disk-write ide-disk mtd nand-disk panic + audio-mute audio-micmute rfkill-any rfkill-none + +Also see **Userspace LEDs** (``Documentation/leds/uleds.rst``). + +Kernel API +========== + +``#include `` + +.. kernel-doc:: block/blk-ledtrig.c + :export: From patchwork Thu Jul 29 01:53:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Pilcher X-Patchwork-Id: 489563 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BD8C1C43216 for ; Thu, 29 Jul 2021 01:54:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9B76961042 for ; Thu, 29 Jul 2021 01:54:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233286AbhG2Bye (ORCPT ); Wed, 28 Jul 2021 21:54:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233153AbhG2Byd (ORCPT ); Wed, 28 Jul 2021 21:54:33 -0400 Received: from mail-ot1-x334.google.com (mail-ot1-x334.google.com [IPv6:2607:f8b0:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19F17C061757; Wed, 28 Jul 2021 18:54:30 -0700 (PDT) Received: by mail-ot1-x334.google.com with SMTP id h63-20020a9d14450000b02904ce97efee36so4189578oth.7; Wed, 28 Jul 2021 18:54:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=1f/SxtepgyjeAa5xFLbjbkeuNqISFy3PTrve9ir2rAc=; b=fCOe4rqRxw6SqV9uk9MpxuiUC9149oYHi6rbVfcg+KgaE/qHn5mwswIDYJ0SwFY+b1 eK8FEBwMJCp5rFT1S7dp42jG+v+nFBG/Us+bUu6uDCHYePeRlFv6MhsMm69K5wjSKaDC ZFolXFJka7u7RyUTzJgt34evLcRvH3aSOZ/PUVpT1k4QA9LSoH70nVLMg0m2N6lWAwYs +3E/4wnPo07PBFMvFtFc9lfx7Z6EmXltY2bNbOd8bupO+Lu1dmzX67pNhZpUgs4dn6XM tQWab73+IufvgU6kDVHJSx6kp62GyIKZf7re2lzyja5O1Z1Zny5etk2WtcAxU+7ohU3o EopA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1f/SxtepgyjeAa5xFLbjbkeuNqISFy3PTrve9ir2rAc=; b=tXmEaB+dAjjvuBcSZU4Fo0tTmXRRDsl/qcdNXpfIZL7sk4wZ2FIXo8tQyp71Nw2u8Z brZLLmnISDHlcv5UpIBypwWCWcGraS+7/ap4NWubzE3pUkHytRZG1CotkamGdRgUVnQx C3YKbwMIgxqhBuaAIzx1Y2LZC/e8Uz/AJVzTyiaPm97OsfZq0Bzhdwb9OIhl4HJKAZ55 c6nfEIF76EL4tFL8j1F4+zMM2lEPEblmdELcTqpDqD/2d9IEGWFWukWr/P27eSkQQCoo rW22M0r/FRiqjxNotiJPEbsR3TLP9rzpqjpRd7oIrrOKz3/nEtToBPsFyYwH63BZh38C Qeww== X-Gm-Message-State: AOAM531BhPebQPm9bqjicBg8GB7byzS70dZrYIVwdGGok+13WchDssa6 kqXSxNcsMW2mN1pSX+/ma6GUxA0ZhEy1B/uD X-Google-Smtp-Source: ABdhPJw8oRvrTnkHJKTijlAFiacDHgpqLu1mz1oZfo3fN8XvtZva5yr2NFp9LnaHTjz3UQrKimxbSw== X-Received: by 2002:a05:6830:23a7:: with SMTP id m7mr1910820ots.17.1627523669477; Wed, 28 Jul 2021 18:54:29 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id c11sm311424otm.37.2021.07.28.18.54.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 18:54:29 -0700 (PDT) From: Ian Pilcher To: linux-block@vger.kernel.org, linux-leds@vger.kernel.org Cc: axboe@kernel.dk, pavel@ucw.cz, linux-kernel@vger.kernel.org, kernelnewbies@kernelnewbies.org, Ian Pilcher Subject: [RFC PATCH 2/8] block: Add block device LED trigger list Date: Wed, 28 Jul 2021 20:53:38 -0500 Message-Id: <20210729015344.3366750-3-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210729015344.3366750-1-arequipeno@gmail.com> References: <20210729015344.3366750-1-arequipeno@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org * New config option (CONFIG_BLK_LED_TRIGGERS) to enable/disable block device LED triggers * New file - block/blk-ledtrig.c * Use a linked list of dynamically allocated triggers. There aren't likely to be that many of them, and the list is only searched when creating/deleting a trigger or setting/clearing a device/trigger association - none of which should occur very often. Signed-off-by: Ian Pilcher --- block/Kconfig | 10 +++++++++ block/Makefile | 1 + block/blk-ledtrig.c | 51 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 block/blk-ledtrig.c diff --git a/block/Kconfig b/block/Kconfig index fd732aede922..051488413d6e 100644 --- a/block/Kconfig +++ b/block/Kconfig @@ -220,6 +220,16 @@ config BLK_INLINE_ENCRYPTION_FALLBACK by falling back to the kernel crypto API when inline encryption hardware is not present. +config BLK_LED_TRIGGERS + bool "Enable block device LED triggers" + depends on LEDS_TRIGGERS + help + Enabling this allows LED triggers to be created and + associated with block devices via sysfs/udev (or an + in-kernel API). These trigers can be used to drive + physical or user-space activity indicators. See + Documentation/block/led-triggers.rst. + menu "Partition Types" source "block/partitions/Kconfig" diff --git a/block/Makefile b/block/Makefile index bfbe4e13ca1e..bcd97ee26462 100644 --- a/block/Makefile +++ b/block/Makefile @@ -42,3 +42,4 @@ obj-$(CONFIG_BLK_SED_OPAL) += sed-opal.o obj-$(CONFIG_BLK_PM) += blk-pm.o obj-$(CONFIG_BLK_INLINE_ENCRYPTION) += keyslot-manager.o blk-crypto.o obj-$(CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK) += blk-crypto-fallback.o +obj-$(CONFIG_BLK_LED_TRIGGERS) += blk-ledtrig.o diff --git a/block/blk-ledtrig.c b/block/blk-ledtrig.c new file mode 100644 index 000000000000..345a3b6bdbc6 --- /dev/null +++ b/block/blk-ledtrig.c @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* + * Block device LED triggers + * + * Copyright 2021 Ian Pilcher + */ + +#include +#include +#include + + +/* + * + * The list of block device LED triggers + * + */ + +struct blk_ledtrig { + struct led_trigger trigger; + struct list_head list_node; + struct mutex refcount_mutex; + int refcount; + char name[]; +}; + +LIST_HEAD(blk_ledtrig_list); +DEFINE_MUTEX(blk_ledtrig_list_mutex); + +static inline +struct blk_ledtrig *blk_ledtrig_from_node(struct list_head *const node) +{ + return container_of(node, struct blk_ledtrig, list_node); +} + +// Caller must hold blk_ledtrig_list_mutex +static struct blk_ledtrig *blk_ledtrig_find(const char *const name, + const size_t len) +{ + struct blk_ledtrig *t; + struct list_head *n; + + list_for_each(n, &blk_ledtrig_list) { + t = blk_ledtrig_from_node(n); + if (strlen(t->name) == len && memcmp(name, t->name, len) == 0) + return t; + } + + return NULL; +} From patchwork Thu Jul 29 01:53:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Pilcher X-Patchwork-Id: 489030 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 211D7C4320E for ; Thu, 29 Jul 2021 01:54:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EAF286108E for ; Thu, 29 Jul 2021 01:54:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233398AbhG2Byh (ORCPT ); Wed, 28 Jul 2021 21:54:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233300AbhG2Bye (ORCPT ); Wed, 28 Jul 2021 21:54:34 -0400 Received: from mail-ot1-x32d.google.com (mail-ot1-x32d.google.com [IPv6:2607:f8b0:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6307C061765; Wed, 28 Jul 2021 18:54:30 -0700 (PDT) Received: by mail-ot1-x32d.google.com with SMTP id 48-20020a9d0bb30000b02904cd671b911bso4219981oth.1; Wed, 28 Jul 2021 18:54:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=e4Cil8UyFveunyfhdg9prQXTKI60qw+NHlxrLHZPnMc=; b=W6tx1rbDfbctaNTfj5z+Y97j2ApUcJJ3DCnHPQo+HTvNvZCewBeIobpazgn/Vm5APW GQg1bntSSJRkojml4beEpbasjH6BVCAOc4u2TyrRMGRfnL64xx6b1/jyACpYEYmBnXsg dJ388clXsqSNOdMBnzcW12Uvd2RQJdXcrEbgR41G+WSAOkp8ZZ7OWtikHF9vuYt0RAqU 6lKZZQQukCiccsH6JUxOzQF7oGmimTRlEwA5WhgyOg0vA4pZJWmzhyKFS7gybrF4RTSt JMLwE/qpB/B/j0LGERe+yruohj7FOr1V2j235PewAp5j3W/DELX9EeSG5UpfzzzC/fu6 knhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=e4Cil8UyFveunyfhdg9prQXTKI60qw+NHlxrLHZPnMc=; b=MnIk0mJNnqa+nLLCN1b5qa4dm8MzS49X290ZiARqX4LdNySRzvZRFVOOIQGQkqAF5N 481XwCGiHRDJJomgoDXE9HgEkbS4sVrB77S/7PRkPnAvCK7Wa+LIHOPL9ZFz/yt/63oS YfD9w0ltVf1q+viJe43NXon8e35cxoLAiEYWDP6LVLooohIRyhdzm/wNAWJHgSrJrCbF p0/PRcrlp5GEMnZeqPIkiRp8kQBIxVB3EozqQtRLX7kds2g9IsfOOcPXJp+oeTSw0W/K ScBYU1QbYNWd59Z947bugFzi/s5uX4pKrqqF7H9wI35yc1y4v/SfrdaSzsx3ZihLyNts ZqnQ== X-Gm-Message-State: AOAM533+CK6xZ7tviVionNlceqwRJZGr0kEHUeG1l8HvmBp3AhQ8aNLg beqYG67TgLy4Kk8Ft/W86MnM02YN39fliZF5 X-Google-Smtp-Source: ABdhPJw82QLWhuo6vRkom8k9uuRZcO4NPiIluShNkHW3TZtfVFNkZAPiQuXfM+DVHaAQsc7SOZRBCw== X-Received: by 2002:a9d:8ac:: with SMTP id 41mr1901869otf.170.1627523670254; Wed, 28 Jul 2021 18:54:30 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id c11sm311424otm.37.2021.07.28.18.54.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 18:54:29 -0700 (PDT) From: Ian Pilcher To: linux-block@vger.kernel.org, linux-leds@vger.kernel.org Cc: axboe@kernel.dk, pavel@ucw.cz, linux-kernel@vger.kernel.org, kernelnewbies@kernelnewbies.org, Ian Pilcher Subject: [RFC PATCH 3/8] block: Add kernel APIs to create & delete block device LED triggers Date: Wed, 28 Jul 2021 20:53:39 -0500 Message-Id: <20210729015344.3366750-4-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210729015344.3366750-1-arequipeno@gmail.com> References: <20210729015344.3366750-1-arequipeno@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org * New file - include/linux/blk-ledtrig.h Signed-off-by: Ian Pilcher --- block/blk-ledtrig.c | 152 ++++++++++++++++++++++++++++++++++++ include/linux/blk-ledtrig.h | 19 +++++ 2 files changed, 171 insertions(+) create mode 100644 include/linux/blk-ledtrig.h diff --git a/block/blk-ledtrig.c b/block/blk-ledtrig.c index 345a3b6bdbc6..c69ea1539336 100644 --- a/block/blk-ledtrig.c +++ b/block/blk-ledtrig.c @@ -6,9 +6,11 @@ * Copyright 2021 Ian Pilcher */ +#include #include #include #include +#include /* @@ -49,3 +51,153 @@ static struct blk_ledtrig *blk_ledtrig_find(const char *const name, return NULL; } + + +/* + * + * Create a new trigger + * + */ + +static int __blk_ledtrig_create(const char *const name, const size_t len) +{ + struct blk_ledtrig *t; + int ret; + + if (len == 0) { + pr_warn("empty name specified for blockdev LED trigger\n"); + ret = -EINVAL; + goto create_exit_return; + } + + ret = mutex_lock_interruptible(&blk_ledtrig_list_mutex); + if (unlikely(ret != 0)) + goto create_exit_return; + + if (blk_ledtrig_find(name, len) != NULL) { + pr_warn("blockdev LED trigger named %.*s already exists\n", + (int)len, name); + ret = -EEXIST; + goto create_exit_unlock_list; + } + + t = kzalloc(sizeof(*t) + len + 1, GFP_KERNEL); + if (unlikely(t == NULL)) { + ret = -ENOMEM; + goto create_exit_unlock_list; + } + + memcpy(t->name, name, len); + t->trigger.name = t->name; + mutex_init(&t->refcount_mutex); + + ret = led_trigger_register(&t->trigger); + if (ret != 0) { + if (likely(ret == -EEXIST)) { + pr_warn("LED trigger named %.*s already exists\n", + (int)len, name); + } + goto create_exit_free; + } + + list_add(&t->list_node, &blk_ledtrig_list); + ret = 0; + +create_exit_free: + if (ret != 0) + kfree(t); +create_exit_unlock_list: + mutex_unlock(&blk_ledtrig_list_mutex); +create_exit_return: + return ret; +} + +/** + * blk_ledtrig_create() - creates a new block device LED trigger + * @name: the name of the new trigger + * + * Context: Process context (can sleep). Takes and releases + * @blk_ledtrig_list_mutex. + * + * Return: 0 on success; -@errno on error + */ +int blk_ledtrig_create(const char *const name) +{ + return __blk_ledtrig_create(name, strlen(name)); +} +EXPORT_SYMBOL_GPL(blk_ledtrig_create); + + +/* + * + * Delete a trigger + * + */ + +static int __blk_ledtrig_delete(const char *const name, const size_t len) +{ + struct blk_ledtrig *t; + int ret; + + if (len == 0) { + pr_warn("empty name specified for blockdev LED trigger\n"); + ret = -EINVAL; + goto delete_exit_return; + } + + ret = mutex_lock_interruptible(&blk_ledtrig_list_mutex); + if (unlikely(ret != 0)) + goto delete_exit_return; + + t = blk_ledtrig_find(name, len); + if (t == NULL) { + pr_warn("blockdev LED trigger named %.*s doesn't exist\n", + (int)len, name); + ret = -ENODEV; + goto delete_exit_unlock_list; + } + + ret = mutex_lock_interruptible(&t->refcount_mutex); + if (unlikely(ret != 0)) + goto delete_exit_unlock_list; + + if (WARN_ON(t->refcount < 0)) { + ret = -EBADFD; + goto delete_exit_unlock_refcount; + } + + if (t->refcount > 0) { + pr_warn("blockdev LED trigger %s still in use\n", t->name); + ret = -EBUSY; + goto delete_exit_unlock_refcount; + } + + led_trigger_unregister(&t->trigger); + list_del(&t->list_node); + + ret = 0; + +delete_exit_unlock_refcount: + mutex_unlock(&t->refcount_mutex); + if (ret == 0) + kfree(t); +delete_exit_unlock_list: + mutex_unlock(&blk_ledtrig_list_mutex); +delete_exit_return: + return ret; +} + +/** + * blk_ledtrig_delete() - deletes a block device LED trigger + * @name: the name of the trigger to be deleted + * + * Context: Process context (can sleep). Takes and releases + * @blk_ledtrig_list_mutex and trigger's @refcount_mutex. + * + * Return: 0 on success; -@errno on error + */ +int blk_ledtrig_delete(const char *const name) +{ + return __blk_ledtrig_delete(name, strlen(name)); +} +EXPORT_SYMBOL_GPL(blk_ledtrig_delete); diff --git a/include/linux/blk-ledtrig.h b/include/linux/blk-ledtrig.h new file mode 100644 index 000000000000..6f73635f65ec --- /dev/null +++ b/include/linux/blk-ledtrig.h @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +/* + * Block device LED triggers + * + * Copyright 2021 Ian Pilcher + */ + +#ifndef _LINUX_BLK_LEDTRIG_H +#define _LINUX_BLK_LEDTRIG_H + +#ifdef CONFIG_BLK_LED_TRIGGERS + +int blk_ledtrig_create(const char *name); +int blk_ledtrig_delete(const char *name); + +#endif // CONFIG_BLK_LED_TRIGGERS + +#endif // _LINUX_BLK_LEDTRIG_H From patchwork Thu Jul 29 01:53:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Pilcher X-Patchwork-Id: 489562 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0CDF4C41537 for ; Thu, 29 Jul 2021 01:54:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 022966101C for ; Thu, 29 Jul 2021 01:54:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233476AbhG2Byj (ORCPT ); Wed, 28 Jul 2021 21:54:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233324AbhG2Bye (ORCPT ); Wed, 28 Jul 2021 21:54:34 -0400 Received: from mail-ot1-x32d.google.com (mail-ot1-x32d.google.com [IPv6:2607:f8b0:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9FA99C0613C1; Wed, 28 Jul 2021 18:54:31 -0700 (PDT) Received: by mail-ot1-x32d.google.com with SMTP id 61-20020a9d0d430000b02903eabfc221a9so4247814oti.0; Wed, 28 Jul 2021 18:54:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=iwbk+n8V82DYCRSAvUIXW0Vdrx9nBNIpoXbvhprm1aU=; b=tPAeipBo6QTlClkDwJ3yEjd1cMxq2ZmPpYnmp44H4/gUBSAxf/yrYO46DG0WkVbQhb nJGB7ygsfUTSaieb9OtgRb3vR0Vr74gv2dgLRcvRA2XWcHNK5f3KxKL4ipP/Lu+eCcwO Myp4cTMrYFJHLqv8aW9RpC1nj9hIB3N0pkQ7BD/CZVSXs7FmSVsV8KCNCSnxec6uVf1G OtlEAqu5q6HUW9/7skhJi3N3pcpjUU1PhxoR1XaXdrRtPVo/WPtUbIFNBdQZJFWqWfPM V2No7psrjQTFOddQ+uLiCoNbAdT7p6DE7wflddqHsCVJl2p7nzMv8iY0Fv4OAMe1Edhp v/3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iwbk+n8V82DYCRSAvUIXW0Vdrx9nBNIpoXbvhprm1aU=; b=jTX3d5oJnr42jWUeq9sFcTOg/7mRj8IxCBqL7ICx3I/+0zyE3ep5MGaI9dRa708u8F UXLGw6xv8dhGn6aEsl9sxoNLWG9HSo4lmYn79HXzd5IntCdhVoRkLtm1p9XPy87CgpLr kYCA9bb8niDg2OJfA/kR3bqqZQNK4so3EDpMZHba8EcVfljtug7IBtQMQ/1mrnRl9eVJ JCxIugoGBzXJnT3pXmnK0HjqpIXYPf/hCePaJUh6bAB/Aqxq4TQOQTegS76rr/OywksT 827QqxtTl/gXxHcBxshmOkowbIR+lcjk8GLTt0jANe0LcqP3T+xlcDR3tDoxvp3spwiR 7vsQ== X-Gm-Message-State: AOAM533r5XSSQOT2l9dCKiJRJpuZwuDtdHVbrvonv131Sgjju2CQzbg3 iRSMoZoCxmO95/fXbeSELDC8rzkvBCRVDryN X-Google-Smtp-Source: ABdhPJy4WA8YQHAm65LnVLGVEyhm3WD7rnl+XeLhSw5+oD0I9pdM+KIFG97DgxlRuxCZlCL7DIhMEA== X-Received: by 2002:a05:6830:40c4:: with SMTP id h4mr1928304otu.75.1627523671009; Wed, 28 Jul 2021 18:54:31 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id c11sm311424otm.37.2021.07.28.18.54.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 18:54:30 -0700 (PDT) From: Ian Pilcher To: linux-block@vger.kernel.org, linux-leds@vger.kernel.org Cc: axboe@kernel.dk, pavel@ucw.cz, linux-kernel@vger.kernel.org, kernelnewbies@kernelnewbies.org, Ian Pilcher Subject: [RFC PATCH 4/8] block: Add block class attributes to manage LED trigger list Date: Wed, 28 Jul 2021 20:53:40 -0500 Message-Id: <20210729015344.3366750-5-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210729015344.3366750-1-arequipeno@gmail.com> References: <20210729015344.3366750-1-arequipeno@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org * New class attributes - /sys/class/block/led_trigger_{new,list,del} * Add init function - blk_ledtrig_init() - to create the attributes in sysfs. Call blk_ledtrig_init() from genhd_device_init() (in block/genhd.c). * New file - block/blk-ledtrig.h Signed-off-by: Ian Pilcher --- block/blk-ledtrig.c | 147 ++++++++++++++++++++++++++++++++++++++++++++ block/blk-ledtrig.h | 22 +++++++ block/genhd.c | 2 + 3 files changed, 171 insertions(+) create mode 100644 block/blk-ledtrig.h diff --git a/block/blk-ledtrig.c b/block/blk-ledtrig.c index c69ea1539336..6392ab4169f9 100644 --- a/block/blk-ledtrig.c +++ b/block/blk-ledtrig.c @@ -7,11 +7,15 @@ */ #include +#include +#include #include #include #include #include +#include "blk-ledtrig.h" + /* * @@ -201,3 +205,146 @@ int blk_ledtrig_delete(const char *const name) return __blk_ledtrig_delete(name, strlen(name)); } EXPORT_SYMBOL_GPL(blk_ledtrig_delete); + + +/* + * + * Class attributes to manage the trigger list + * + */ + +static ssize_t blk_ledtrig_attr_store(struct class *, struct class_attribute *, + const char *, const size_t); +static ssize_t blk_ledtrig_list_show(struct class *, struct class_attribute *, + char *); + +static struct class_attribute blk_ledtrig_attr_new = + __ATTR(led_trigger_new, 0200, 0, blk_ledtrig_attr_store); + +static struct class_attribute blk_ledtrig_attr_del = + __ATTR(led_trigger_del, 0200, 0, blk_ledtrig_attr_store); + +static struct class_attribute blk_ledtrig_attr_list = + __ATTR(led_trigger_list, 0444, blk_ledtrig_list_show, 0); + +// Returns a pointer to the first non-whitespace character in s (or a pointer +// to the terminating nul). +static const char *blk_ledtrig_skip_whitespace(const char *s) +{ + while (*s != 0 && isspace(*s)) + ++s; + + return s; +} + +// Returns a pointer to the first whitespace character in s (or a pointer to +// the terminating nul), which is effectively a pointer to the position *after* +// the last character in the non-whitespace token at the beginning of s. (s is +// expected to be the result of a previous call to blk_ledtrig_skip_whitespace.) +static const char *blk_ledtrig_find_whitespace(const char *s) +{ + while (*s != 0 && !isspace(*s)) + ++s; + + return s; +} + +static ssize_t blk_ledtrig_attr_store(struct class *const class, + struct class_attribute *const attr, + const char *const buf, + const size_t count) +{ + const char *const name = blk_ledtrig_skip_whitespace(buf); + const char *const endp = blk_ledtrig_find_whitespace(name); + const ptrdiff_t name_len = endp - name; // always >= 0 + int ret; + + if (attr == &blk_ledtrig_attr_new) + ret = __blk_ledtrig_create(name, name_len); + else // attr == &blk_ledtrig_attr_del + ret = __blk_ledtrig_delete(name, name_len); + + if (ret < 0) + return ret; + + // Avoid potential "empty name" error by skipping whitespace + // to next token or terminating nul + return blk_ledtrig_skip_whitespace(endp) - buf; +} + +static ssize_t blk_ledtrig_list_show(struct class *const class, + struct class_attribute *const attr, + char *const buf) +{ + struct list_head *n; + int ret, c = 0; + + ret = mutex_lock_interruptible(&blk_ledtrig_list_mutex); + if (unlikely(ret != 0)) + goto list_exit_return; + + list_for_each(n, &blk_ledtrig_list) { + + struct blk_ledtrig *const t = blk_ledtrig_from_node(n); + int refcount; + + ret = mutex_lock_interruptible(&t->refcount_mutex); + if (unlikely(ret != 0)) + goto list_exit_unlock_list; + + refcount = t->refcount; + + mutex_unlock(&t->refcount_mutex); + + ret = snprintf(buf + c, PAGE_SIZE - c, "%s: %d\n", + t->name, refcount); + if (unlikely(ret < 0)) + goto list_exit_unlock_list; + + c += ret; + if (unlikely(c >= PAGE_SIZE)) { + ret = -EOVERFLOW; + goto list_exit_unlock_list; + } + } + + ret = c; + +list_exit_unlock_list: + mutex_unlock(&blk_ledtrig_list_mutex); +list_exit_return: + return ret; +} + + +/* + * + * Initialization - create the class attributes + * + */ + +void __init blk_ledtrig_init(void) +{ + int ret; + + ret = class_create_file(&block_class, &blk_ledtrig_attr_new); + if (unlikely(ret != 0)) + goto init_error_new; + + ret = class_create_file(&block_class, &blk_ledtrig_attr_del); + if (unlikely(ret != 0)) + goto init_error_del; + + ret = class_create_file(&block_class, &blk_ledtrig_attr_list); + if (unlikely(ret != 0)) + goto init_error_list; + + return; + +init_error_list: + class_remove_file(&block_class, &blk_ledtrig_attr_del); +init_error_del: + class_remove_file(&block_class, &blk_ledtrig_attr_new); +init_error_new: + pr_err("failed to initialize blkdev LED triggers (%d)\n", ret); +} diff --git a/block/blk-ledtrig.h b/block/blk-ledtrig.h new file mode 100644 index 000000000000..894843249deb --- /dev/null +++ b/block/blk-ledtrig.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +/* + * Block device LED triggers + * + * Copyright 2021 Ian Pilcher + */ + +#ifndef _BLOCK_BLK_LEDTRIG_H +#define _BLOCK_BLK_LEDTRIG_H + +#ifdef CONFIG_BLK_LED_TRIGGERS + +void blk_ledtrig_init(void); + +#else // CONFIG_BLK_LED_TRIGGERS + +static inline void blk_ledtrig_init(void) {} + +#endif // CONFIG_BLK_LED_TRIGGERS + +#endif // _BLOCK_BLK_LEDTRIG_H diff --git a/block/genhd.c b/block/genhd.c index af4d2ab4a633..d0b1d8f743ae 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -26,6 +26,7 @@ #include #include "blk.h" +#include "blk-ledtrig.h" static struct kobject *block_depr; @@ -824,6 +825,7 @@ static int __init genhd_device_init(void) if (unlikely(error)) return error; blk_dev_init(); + blk_ledtrig_init(); register_blkdev(BLOCK_EXT_MAJOR, "blkext"); From patchwork Thu Jul 29 01:53:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Pilcher X-Patchwork-Id: 489561 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C841CC19F33 for ; Thu, 29 Jul 2021 01:54:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A85B26101C for ; Thu, 29 Jul 2021 01:54:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233488AbhG2Byk (ORCPT ); Wed, 28 Jul 2021 21:54:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233342AbhG2Byf (ORCPT ); Wed, 28 Jul 2021 21:54:35 -0400 Received: from mail-oi1-x236.google.com (mail-oi1-x236.google.com [IPv6:2607:f8b0:4864:20::236]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76CD3C061757; Wed, 28 Jul 2021 18:54:32 -0700 (PDT) Received: by mail-oi1-x236.google.com with SMTP id y18so6368257oiv.3; Wed, 28 Jul 2021 18:54:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IXsGqm421WCEa+uNZfI8JQ6N8WTvSJdhDMDnpsXLO40=; b=sbO+GKQPOuYSoVEj0dWJThzQL+8BSjmWLzmMbnzB9QZaarb4p4F7qJUSyPP4lPWic+ xUEJPigJ03l7hMB2zpGHSllrSoxJKyatDiLfPd+WUfgDygXFVffOV1TZKLl5jWXwZlse jrQKTbmyNEE7yqaH/LoYy6v17yks+Q/UcEAasqxVYch+QOrzxFyIj1bYLYtvlqG1TNbn JcEfcQC+uKWF7pGiAkAG1ClCUHojl5dkcF4BcZvWss9eAF8TIGVjz0iybilrdHNc4EhS sPmmqQOsjMs0IcuD6mJr1k2D0dG+cmNEJsOUHXc0Ud0Wdc4xVm/PO+H9zAWs7fCCHpQh LuvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IXsGqm421WCEa+uNZfI8JQ6N8WTvSJdhDMDnpsXLO40=; b=nlRO+hUsJ9uEotn+ELaD/Hza1QLjLYJrBfYtk35521jymNxq7RGSQWgnhx5i4pPm6p GmxYDHyENbQPF6XEjpow7rqdamlQW9lD82SMcp7kZGWwIIWBBFttY7swWLjPfDJ4s1Rw gNMxGYifxFVMyeTCbEN9iaTtOopBnofwdIefVOwhRell0Gono81Te6omBMRJaXju8EPG iqVGoVzIGVzMrqKLlK2+Pj0puYMLs1M2uWpE4f4pxlqk32aZ0Rpq/zN0chAE4iakXCzJ 18DXNkQ/+9vxKOqj+PpPe0sRaToXF+ff4lDp5vR+Otbvduswhr+PCil0++8NdPsjpLxC UMbg== X-Gm-Message-State: AOAM533BHOqDbqHK57QJzCl5pCA7Z9ENEWdrcbfHKX8l+n9r+P7/4Ysp N681OCIb1YlaDO8Ac8Vr6WabaP0v++nSvz+q X-Google-Smtp-Source: ABdhPJynUEtutX2shrkIN9s2qUx8+ygnzu8SLmc3+T0FSAsIUtMwMV62UOTZ7ZfUyPe4Qa3IHsW9Yw== X-Received: by 2002:aca:fcc3:: with SMTP id a186mr7975388oii.105.1627523671883; Wed, 28 Jul 2021 18:54:31 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id c11sm311424otm.37.2021.07.28.18.54.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 18:54:31 -0700 (PDT) From: Ian Pilcher To: linux-block@vger.kernel.org, linux-leds@vger.kernel.org Cc: axboe@kernel.dk, pavel@ucw.cz, linux-kernel@vger.kernel.org, kernelnewbies@kernelnewbies.org, Ian Pilcher Subject: [RFC PATCH 5/8] block: Add block device LED trigger info to struct genhd Date: Wed, 28 Jul 2021 20:53:41 -0500 Message-Id: <20210729015344.3366750-6-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210729015344.3366750-1-arequipeno@gmail.com> References: <20210729015344.3366750-1-arequipeno@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org * Initialize trigger info when device is added. Signed-off-by: Ian Pilcher --- block/blk-ledtrig.h | 7 +++++++ block/genhd.c | 1 + include/linux/genhd.h | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/block/blk-ledtrig.h b/block/blk-ledtrig.h index 894843249deb..5854b21a210c 100644 --- a/block/blk-ledtrig.h +++ b/block/blk-ledtrig.h @@ -13,9 +13,16 @@ void blk_ledtrig_init(void); +static inline void blk_ledtrig_disk_init(struct gendisk *const gd) +{ + gd->ledtrig = NULL; + mutex_init(&gd->ledtrig_mutex); +} + #else // CONFIG_BLK_LED_TRIGGERS static inline void blk_ledtrig_init(void) {} +static inline void blk_ledtrig_disk_init(const struct gendisk *gd) {} #endif // CONFIG_BLK_LED_TRIGGERS diff --git a/block/genhd.c b/block/genhd.c index d0b1d8f743ae..420325447c5d 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -540,6 +540,7 @@ static void __device_add_disk(struct device *parent, struct gendisk *disk, disk_add_events(disk); blk_integrity_add(disk); + blk_ledtrig_disk_init(disk); } void device_add_disk(struct device *parent, struct gendisk *disk, diff --git a/include/linux/genhd.h b/include/linux/genhd.h index 13b34177cc85..3409334c9b4c 100644 --- a/include/linux/genhd.h +++ b/include/linux/genhd.h @@ -169,6 +169,10 @@ struct gendisk { #if IS_ENABLED(CONFIG_CDROM) struct cdrom_device_info *cdi; #endif +#ifdef CONFIG_BLK_LED_TRIGGERS + struct blk_ledtrig *ledtrig; + struct mutex ledtrig_mutex; +#endif /* CONFIG_BLK_LED_TRIGGERS */ int node_id; struct badblocks *bb; struct lockdep_map lockdep_map; From patchwork Thu Jul 29 01:53:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Pilcher X-Patchwork-Id: 489029 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 50789C4338F for ; Thu, 29 Jul 2021 01:54:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3275061042 for ; Thu, 29 Jul 2021 01:54:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233528AbhG2Byl (ORCPT ); Wed, 28 Jul 2021 21:54:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233370AbhG2Byg (ORCPT ); Wed, 28 Jul 2021 21:54:36 -0400 Received: from mail-ot1-x32d.google.com (mail-ot1-x32d.google.com [IPv6:2607:f8b0:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 481B5C0613CF; Wed, 28 Jul 2021 18:54:33 -0700 (PDT) Received: by mail-ot1-x32d.google.com with SMTP id f20-20020a9d6c140000b02904bb9756274cso4184266otq.6; Wed, 28 Jul 2021 18:54:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=p/h9CNhDEWaVYT21D38h0CBGh+E5bTlq6KWJZsooWmc=; b=oKQ5XMdqLNJK1CchH8QRSYF5bWz65GClpM9gYk/AR/X++pukl4wVzJAfMBpvRiDHyi i8T5mUidgHraCdyPjy/1z/ZzPLJSxzOxtCOm274vMx04xqtPbAq1GpY0EHq1qdOxVIWL wtJ0XPPismyXN+10tjE3DkOFVXFGVtCP8PMmk4ijGa8iL4MWW5twGkzaZgjAUGkH5Ltg h8iqqzadL3r4rXBvqT8UbfsuRlH6RuRwjASnrxxXz4v4Z7OUnWhddhxTikh1smbJR6Fb iIL12gPQnpW/WQZdC+CFZeRtnPv5XoVXtX+L5ZzvW4h6tj+EV4OXfyMV/iCvx99puKl8 aqyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=p/h9CNhDEWaVYT21D38h0CBGh+E5bTlq6KWJZsooWmc=; b=H2/hTJZbdAlTqzKfYJtVv6pc/XHkPP5T4gaNkkUWl8pV6yG6Ln5kAZF1PeneMV/dBQ 5lPI09s/q1DpU2R7M4o2KvrgGK3DAyPOStd8cHj+eHoeDDPmwx4FldYcSw0o+2iBlYVF QR5rpG2+s7W9evbzCGlgO+gARacyqIzOowDha9er1o770V6mYaaOs7L1W6UoySVzCGfO mI29yg4ivrUb4W+2D1utUGJtyooQaHuZaDohfm/w4NUPKSWVUmkKRyA5fRu7ve1BEz3A m2rq6UNMWIEmOZu7l/blP4yCbMY6RkztMm/TM7hhsEmNlXg8t76mEn9VrjaLiMB9Q8Ue AvHQ== X-Gm-Message-State: AOAM533HfZBMVd0YHsH09L7zA82/Gy0DE3jLKTrApxv1vyzmPDY0E4kY 2v5tznBVxyi5lhqphUTFhFLBKS9WKcz/6KQa X-Google-Smtp-Source: ABdhPJwV1SZfY/OcfRoQmrShjgEfsL5nfsPervIgTuOpn3XNxlelwr4Ao/A2hCxFCd+nAFNtBlR6nA== X-Received: by 2002:a05:6830:b:: with SMTP id c11mr1918695otp.149.1627523672666; Wed, 28 Jul 2021 18:54:32 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id c11sm311424otm.37.2021.07.28.18.54.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 18:54:32 -0700 (PDT) From: Ian Pilcher To: linux-block@vger.kernel.org, linux-leds@vger.kernel.org Cc: axboe@kernel.dk, pavel@ucw.cz, linux-kernel@vger.kernel.org, kernelnewbies@kernelnewbies.org, Ian Pilcher Subject: [RFC PATCH 6/8] block: Add kernel APIs to set & clear per-block device LED triggers Date: Wed, 28 Jul 2021 20:53:42 -0500 Message-Id: <20210729015344.3366750-7-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210729015344.3366750-1-arequipeno@gmail.com> References: <20210729015344.3366750-1-arequipeno@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org * Clear LED trigger (decrement trigger reference count) when device is deleted, e.g. when a USB disk is unplugged. Signed-off-by: Ian Pilcher --- block/blk-ledtrig.c | 131 ++++++++++++++++++++++++++++++++++++ block/blk-ledtrig.h | 5 ++ block/genhd.c | 2 + include/linux/blk-ledtrig.h | 5 ++ 4 files changed, 143 insertions(+) diff --git a/block/blk-ledtrig.c b/block/blk-ledtrig.c index 6392ab4169f9..7c8fdff88683 100644 --- a/block/blk-ledtrig.c +++ b/block/blk-ledtrig.c @@ -348,3 +348,134 @@ void __init blk_ledtrig_init(void) init_error_new: pr_err("failed to initialize blkdev LED triggers (%d)\n", ret); } + + +/* + * + * Set a device trigger + * + */ + +static int __blk_ledtrig_set(struct gendisk *const gd, const char *const name, + const size_t name_len) +{ + struct blk_ledtrig *t; + bool already_set; + int ret; + + ret = mutex_lock_interruptible(&blk_ledtrig_list_mutex); + if (unlikely(ret != 0)) + goto set_exit_return; + + t = blk_ledtrig_find(name, name_len); + if (t == NULL) { + pr_warn("blockdev LED trigger named %.*s doesn't exist\n", + (int)name_len, name); + ret = -ENODEV; + goto set_exit_unlock_list; + } + + ret = mutex_lock_interruptible(&t->refcount_mutex); + if (unlikely(ret != 0)) + goto set_exit_unlock_list; + + // Holding the refcount mutex blocks __blk_ledtrig_delete, so we don't + // actually need to hold the list mutex anymore, but it makes the flow + // much simpler to do so + + if (WARN_ON_ONCE(t->refcount == INT_MAX)) { + ret = -ERANGE; + goto set_exit_unlock_refcount; + } + + ret = mutex_lock_interruptible(&gd->ledtrig_mutex); + if (unlikely(ret != 0)) + goto set_exit_unlock_refcount; + + if (gd->ledtrig == NULL) { + already_set = false; + gd->ledtrig = t; + } else { + already_set = true; + } + + mutex_unlock(&gd->ledtrig_mutex); + + if (already_set) { + pr_warn("blockdev trigger for %s already set\n", + gd->disk_name); + ret = -EBUSY; + goto set_exit_unlock_refcount; + } + + ++(t->refcount); + ret = 0; + +set_exit_unlock_refcount: + mutex_unlock(&t->refcount_mutex); +set_exit_unlock_list: + mutex_unlock(&blk_ledtrig_list_mutex); +set_exit_return: + return ret; +} + +/** + * blk_ledtrig_set() - set the LED trigger for a block device + * @gd: the block device + * @name: the name of the LED trigger + * + * Context: Process context (can sleep). Takes and releases + * @blk_ledtrig_list_mutex, trigger's @refcount_mutex, + * and @gd->ledtrig_mutex. + * + * Return: 0 on success; -@errno on error + */ +int blk_ledtrig_set(struct gendisk *const gd, const char *const name) +{ + return __blk_ledtrig_set(gd, name, strlen(name)); +} +EXPORT_SYMBOL_GPL(blk_ledtrig_set); + + +/* + * + * Clear a device trigger + * + */ + +/** + * blk_ledtrig_clear() - clear the LED trigger of a block device + * @gd: the block device + * + * Context: Process context (can sleep). Takes and releases + * @gd->ledtrig_mutex and @gd->ledtrig->refcount_mutex. + * + * Return: @true if the trigger was actually cleared; @false if it wasn't set + */ +bool blk_ledtrig_clear(struct gendisk *const gd) +{ + struct blk_ledtrig *t; + bool changed; + int new_refcount; + + mutex_lock(&gd->ledtrig_mutex); + + t = gd->ledtrig; + if (t == NULL) { + changed = false; + goto clear_exit_unlock_ledtrig; + } + + mutex_lock(&t->refcount_mutex); + new_refcount = --(t->refcount); + mutex_unlock(&t->refcount_mutex); + + gd->ledtrig = NULL; + changed = true; + +clear_exit_unlock_ledtrig: + mutex_unlock(&gd->ledtrig_mutex); + WARN_ON(changed && (new_refcount < 0)); + return changed; +} +EXPORT_SYMBOL_GPL(blk_ledtrig_clear); diff --git a/block/blk-ledtrig.h b/block/blk-ledtrig.h index 5854b21a210c..9b718d45783f 100644 --- a/block/blk-ledtrig.h +++ b/block/blk-ledtrig.h @@ -24,6 +24,11 @@ static inline void blk_ledtrig_disk_init(struct gendisk *const gd) static inline void blk_ledtrig_init(void) {} static inline void blk_ledtrig_disk_init(const struct gendisk *gd) {} +// Real function (declared in include/linux/blk-ledtrig.h) returns a bool. +// This is only here for del_gendisk() (in genhd.c), which doesn't check +// the return value. +static inline void blk_ledtrig_clear(const struct gendisk *gd) {} + #endif // CONFIG_BLK_LED_TRIGGERS #endif // _BLOCK_BLK_LEDTRIG_H diff --git a/block/genhd.c b/block/genhd.c index 420325447c5d..fb1617f21d79 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "blk.h" #include "blk-ledtrig.h" @@ -583,6 +584,7 @@ void del_gendisk(struct gendisk *disk) if (WARN_ON_ONCE(!disk->queue)) return; + blk_ledtrig_clear(disk); blk_integrity_del(disk); disk_del_events(disk); diff --git a/include/linux/blk-ledtrig.h b/include/linux/blk-ledtrig.h index 6f73635f65ec..4ab4658df280 100644 --- a/include/linux/blk-ledtrig.h +++ b/include/linux/blk-ledtrig.h @@ -11,8 +11,13 @@ #ifdef CONFIG_BLK_LED_TRIGGERS +#include +#include + int blk_ledtrig_create(const char *name); int blk_ledtrig_delete(const char *name); +int blk_ledtrig_set(struct gendisk *const gd, const char *const name); +bool blk_ledtrig_clear(struct gendisk *const gd); #endif // CONFIG_BLK_LED_TRIGGERS From patchwork Thu Jul 29 01:53:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Pilcher X-Patchwork-Id: 489560 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7E87EC432BE for ; Thu, 29 Jul 2021 01:54:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6736D6101C for ; Thu, 29 Jul 2021 01:54:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233443AbhG2Byr (ORCPT ); Wed, 28 Jul 2021 21:54:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233197AbhG2Byh (ORCPT ); Wed, 28 Jul 2021 21:54:37 -0400 Received: from mail-oo1-xc36.google.com (mail-oo1-xc36.google.com [IPv6:2607:f8b0:4864:20::c36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14B23C061765; Wed, 28 Jul 2021 18:54:34 -0700 (PDT) Received: by mail-oo1-xc36.google.com with SMTP id y11-20020a4ade0b0000b029024b4146e2f5so1161537oot.1; Wed, 28 Jul 2021 18:54:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UKOzPifcOxboRw82QSFcbD/HkRKqBfmFXN/TjZK3tNM=; b=Xxo4L1WAjsxK0AkStEI1tWU1wSyxl/+SCmyVmtSCGxJz7LsE5EadaoAUSOZsjpln3y o2a1d6Cf0uEJoWdVvIYj7BV6fwVMPfqhO82Ux+pgE5ckRNEJwFoy5ldpwTTBb21kDxd6 G1z0/OHY+dNjQ3KhIfNzIdzuKlRhe92RUcWHccVZYp6hNMiTaLOMVMow9eWFVU98wvPT DNibWu3exdtdPsoJE9Eh28hi2pAzqIv7pLBzSEYtV+MmtTym98pWI3xvbxTWDbaDhEpB vmgrsnkIPob/F1Epz0H+DIw4foMHsee32b2UaZDgN4VaSW4qVXK9L3CE6qi5BJ9rIBCZ 4Mug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UKOzPifcOxboRw82QSFcbD/HkRKqBfmFXN/TjZK3tNM=; b=Q8oJ4KiTuJquFfIxSH4IrFlaB7S17GjMCyWcocWvQQIG+iXIQpPAFKP57s7lypaLI3 CsBnVBXmAUPOincELMx+yj5VvLuoN4UDEFtS3sNf4wkEuFacS0hdO+gV4hotFhmGFcE/ iaPKj53Td73iPbnybQ1+MOQWAW5r813MvFqL7OTl0CH4Rm+I9Euk9gbXnkefItrdP7GI G7YvOM8HQeq0Cx9hsFsQLLeUaJzh30sVK51PQ4plZKSVRQGeX3bcQrzDL0uoFVx8YOux fWavm4JwxSHduVHbNw6Ubbfp29a5UXQM6TA2qDCGDbTzUhV4qkIgqrMsrm46yY28tdLi uceQ== X-Gm-Message-State: AOAM533vBOfGnGj3uEvRxWyhtuIFTm6VKNkj+nt5OX+ZPSfTVRtJl/H0 smws0a9rVTeYZSAEk8oXLp5+393Fi+j2vJyR X-Google-Smtp-Source: ABdhPJx62adXAx+HuWv/EHgbkjiBZk3d7ZmqLFbNj2fEXyRMYzp6AU1fgiKFXguabEtlV0l1xhiu5g== X-Received: by 2002:a4a:e089:: with SMTP id w9mr1456131oos.50.1627523673391; Wed, 28 Jul 2021 18:54:33 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id c11sm311424otm.37.2021.07.28.18.54.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 18:54:33 -0700 (PDT) From: Ian Pilcher To: linux-block@vger.kernel.org, linux-leds@vger.kernel.org Cc: axboe@kernel.dk, pavel@ucw.cz, linux-kernel@vger.kernel.org, kernelnewbies@kernelnewbies.org, Ian Pilcher Subject: [RFC PATCH 7/8] block: Add block device attributes to set & clear LED triggers Date: Wed, 28 Jul 2021 20:53:43 -0500 Message-Id: <20210729015344.3366750-8-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210729015344.3366750-1-arequipeno@gmail.com> References: <20210729015344.3366750-1-arequipeno@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org * Set/store functions in block/blk-ledtrig.c * Add device attributes to disk_attrs in block/genhd.c Signed-off-by: Ian Pilcher --- block/blk-ledtrig.c | 65 +++++++++++++++++++++++++++++++++++++++++++++ block/blk-ledtrig.h | 8 ++++++ block/genhd.c | 9 +++++++ 3 files changed, 82 insertions(+) diff --git a/block/blk-ledtrig.c b/block/blk-ledtrig.c index 7c8fdff88683..2d324df45149 100644 --- a/block/blk-ledtrig.c +++ b/block/blk-ledtrig.c @@ -479,3 +479,68 @@ bool blk_ledtrig_clear(struct gendisk *const gd) return changed; } EXPORT_SYMBOL_GPL(blk_ledtrig_clear); + + +/* + * + * Set, clear & show LED triggers via sysfs device attributes + * + * (See dev_attr_led_trigger and disk_attrs in genhd.c) + * + */ + +ssize_t blk_ledtrig_devattr_store(struct device *const dev, + struct device_attribute *const attr, + const char *const buf, const size_t count) +{ + struct gendisk *const gd = dev_to_disk(dev); + const char *const name = blk_ledtrig_skip_whitespace(buf); + const char *const endp = blk_ledtrig_find_whitespace(name); + const ptrdiff_t name_len = endp - name; // always >= 0 + int ret; + + if (name_len == 0) + ret = blk_ledtrig_clear(gd); + else + ret = __blk_ledtrig_set(gd, name, name_len); + + if (ret < 0) + return ret; + + return blk_ledtrig_skip_whitespace(endp) - buf; +} + +ssize_t blk_ledtrig_devattr_show(struct device *const dev, + struct device_attribute *const attr, + char *const buf) +{ + struct gendisk *const gd = dev_to_disk(dev); + const struct blk_ledtrig *t; + size_t name_len; + int ret; + + ret = mutex_lock_interruptible(&gd->ledtrig_mutex); + if (unlikely(ret != 0)) + return ret; + + t = gd->ledtrig; + + if (t != NULL) { + name_len = strlen(t->name); + if (likely(name_len < PAGE_SIZE - 1)) + memcpy(buf, t->name, name_len); + } + + mutex_unlock(&gd->ledtrig_mutex); + + if (t == NULL) + return sprintf(buf, "(none)\n"); + + if (unlikely(name_len >= PAGE_SIZE - 1)) + return -EOVERFLOW; + + buf[name_len] = '\n'; + buf[name_len + 1] = 0; + + return (ssize_t)(name_len + 1); +} diff --git a/block/blk-ledtrig.h b/block/blk-ledtrig.h index 9b718d45783f..5d228905edbf 100644 --- a/block/blk-ledtrig.h +++ b/block/blk-ledtrig.h @@ -19,6 +19,14 @@ static inline void blk_ledtrig_disk_init(struct gendisk *const gd) mutex_init(&gd->ledtrig_mutex); } +ssize_t blk_ledtrig_devattr_store(struct device *const dev, + struct device_attribute *const attr, + const char *const buf, const size_t count); + +ssize_t blk_ledtrig_devattr_show(struct device *const dev, + struct device_attribute *const attr, + char *const buf); + #else // CONFIG_BLK_LED_TRIGGERS static inline void blk_ledtrig_init(void) {} diff --git a/block/genhd.c b/block/genhd.c index fb1617f21d79..fd37efe74d48 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -1014,6 +1014,12 @@ static struct device_attribute dev_attr_fail_timeout = __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store); #endif +#ifdef CONFIG_BLK_LED_TRIGGERS +static struct device_attribute dev_attr_led_trigger = + __ATTR(led_trigger, 0644, + blk_ledtrig_devattr_show, blk_ledtrig_devattr_store); +#endif + static struct attribute *disk_attrs[] = { &dev_attr_range.attr, &dev_attr_ext_range.attr, @@ -1035,6 +1041,9 @@ static struct attribute *disk_attrs[] = { #endif #ifdef CONFIG_FAIL_IO_TIMEOUT &dev_attr_fail_timeout.attr, +#endif +#ifdef CONFIG_BLK_LED_TRIGGERS + &dev_attr_led_trigger.attr, #endif NULL }; From patchwork Thu Jul 29 01:53:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Pilcher X-Patchwork-Id: 489028 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE4DFC4320E for ; Thu, 29 Jul 2021 01:54:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 99EBB6101C for ; Thu, 29 Jul 2021 01:54:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233442AbhG2Byq (ORCPT ); Wed, 28 Jul 2021 21:54:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233440AbhG2Byh (ORCPT ); Wed, 28 Jul 2021 21:54:37 -0400 Received: from mail-oi1-x22f.google.com (mail-oi1-x22f.google.com [IPv6:2607:f8b0:4864:20::22f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F99BC0613C1; Wed, 28 Jul 2021 18:54:35 -0700 (PDT) Received: by mail-oi1-x22f.google.com with SMTP id a19so6351530oiw.6; Wed, 28 Jul 2021 18:54:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=As8Tkf4mRo7Mi47sb2gawTIsMDIBW35dWzDfuJXzf24=; b=KAXxg7WDVZEiBctmG67MeAut2sjCZTRyAlgzgozM0u4dV10MjAmjS86P3FlU+oRh+d ghx90pJrqPDbf7Bp8D/UAD3HFl51KaHhxydnKfpWLyaL/lMrpvgZ3tOl4MwjPhzpuU+m HPQNjmomvkHC98rUTLOnoCCxeLQfa+I167ai5XSSD2UE721s6UCl8ozJgaYAwteTKkc/ 10Ba3iV2uv7CC4zjuuYBIFTWALir0n3mP/oci/LS9L7K0k2CHxZKeMPFwp89VepBYeqQ +aK0bJS6GnSwYJZbCRS02GZriHFHuWNQZ5GesGwVxHEC+cDkoT3Dy8sqWFM86au913Bh oeVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=As8Tkf4mRo7Mi47sb2gawTIsMDIBW35dWzDfuJXzf24=; b=KO4NOOonAyouTT5YH1+vMsSQHPCB93ltYX3Nb0cD/IL56VbrzTmhNxMayjFrjFSLMW 8p07DfPQCrBqCSETQCAVDgnxCd3I6zILk6axhdS1OHp/IhrXFbtS8k7Qh/m48en7MiOW 1HCt8gGOIeFpAYmnqfW/wewPSxT3KZmVPJv3ar3mbTBGWLjyDSCi1zBRzb8Xq9JU+5MT nMmKRvdV8tC2C8VvdB/B3Pla2pomVyhLSf6KiWQvgZdLUxB12LeW7ApvKV7XcGxB31Jm XWi52H9joQVkHdhSu22h5SmE03Y9MtB6ILUgpDCr5oJRi4g83RU7k6xz1FRtSChMGS+u 4x3Q== X-Gm-Message-State: AOAM532cudXDXSAyVdc9ZZj55YT7cyia7De7T6/nLLIL5qvFJSjrX1uw id92Rp5hF6H9Ve1e6/DURtZLqAUbxHFieS/S X-Google-Smtp-Source: ABdhPJxzLQsJ5CMubDia3i29fHRTGMkjAZX1g4DtO40aQk4OXdjunp+l7c/f+gIhfdtLP3dwYUm99g== X-Received: by 2002:aca:d11:: with SMTP id 17mr8341809oin.19.1627523674550; Wed, 28 Jul 2021 18:54:34 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id c11sm311424otm.37.2021.07.28.18.54.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 18:54:33 -0700 (PDT) From: Ian Pilcher To: linux-block@vger.kernel.org, linux-leds@vger.kernel.org Cc: axboe@kernel.dk, pavel@ucw.cz, linux-kernel@vger.kernel.org, kernelnewbies@kernelnewbies.org, Ian Pilcher Subject: [RFC PATCH 8/8] block: Blink device LED when request is sent to low-level driver Date: Wed, 28 Jul 2021 20:53:44 -0500 Message-Id: <20210729015344.3366750-9-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210729015344.3366750-1-arequipeno@gmail.com> References: <20210729015344.3366750-1-arequipeno@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org * Don't wait to lock the device's LED trigger mutex; just don't blink the LED this time if it can't be locked right away, i.e. if mutex_trylock() fails. Signed-off-by: Ian Pilcher --- block/blk-ledtrig.c | 24 ++++++++++++++++++++++++ block/blk-ledtrig.h | 9 +++++++++ block/blk-mq.c | 2 ++ 3 files changed, 35 insertions(+) diff --git a/block/blk-ledtrig.c b/block/blk-ledtrig.c index 2d324df45149..1b475530ce6c 100644 --- a/block/blk-ledtrig.c +++ b/block/blk-ledtrig.c @@ -544,3 +544,27 @@ ssize_t blk_ledtrig_devattr_show(struct device *const dev, return (ssize_t)(name_len + 1); } + + +/* + * + * Try to blink an LED + * + */ + +void __blk_ledtrig_try_blink(struct gendisk *const gd) +{ + if (mutex_trylock(&gd->ledtrig_mutex)) { + + if (gd->ledtrig != NULL) { + + unsigned long delay_on = 75; + unsigned long delay_off = 25; + + led_trigger_blink_oneshot(&gd->ledtrig->trigger, + &delay_on, &delay_off, 0); + } + + mutex_unlock(&gd->ledtrig_mutex); + } +} diff --git a/block/blk-ledtrig.h b/block/blk-ledtrig.h index 5d228905edbf..146deda92a8e 100644 --- a/block/blk-ledtrig.h +++ b/block/blk-ledtrig.h @@ -27,10 +27,19 @@ ssize_t blk_ledtrig_devattr_show(struct device *const dev, struct device_attribute *const attr, char *const buf); +void __blk_ledtrig_try_blink(struct gendisk *gd); + +static inline void blk_ledtrig_try_blink(struct gendisk *const gd) +{ + if (gd != NULL) + __blk_ledtrig_try_blink(gd); +} + #else // CONFIG_BLK_LED_TRIGGERS static inline void blk_ledtrig_init(void) {} static inline void blk_ledtrig_disk_init(const struct gendisk *gd) {} +static inline void blk_ledtrig_try_blink(const struct gendisk *gd) {} // Real function (declared in include/linux/blk-ledtrig.h) returns a bool. // This is only here for del_gendisk() (in genhd.c), which doesn't check diff --git a/block/blk-mq.c b/block/blk-mq.c index 2c4ac51e54eb..5593ece7b676 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -40,6 +40,7 @@ #include "blk-stat.h" #include "blk-mq-sched.h" #include "blk-rq-qos.h" +#include "blk-ledtrig.h" static DEFINE_PER_CPU(struct llist_head, blk_cpu_done); @@ -1381,6 +1382,7 @@ bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list, switch (ret) { case BLK_STS_OK: queued++; + blk_ledtrig_try_blink(rq->rq_disk); break; case BLK_STS_RESOURCE: case BLK_STS_DEV_RESOURCE: