From patchwork Tue May 8 10:05:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= X-Patchwork-Id: 910086 Return-Path: X-Original-To: incoming-imx@patchwork.ozlabs.org Delivered-To: patchwork-incoming-imx@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-arm-kernel-bounces+incoming-imx=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="SZ9VTj0X"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=infradead.org header.i=@infradead.org header.b="YPI415RT"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40gFd41Mx1z9ryk for ; Tue, 8 May 2018 20:11:20 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=qNAKwRWIwf6fASoRFmoMCwFTlPEJC4ftkNwtl1y/Ry0=; b=SZ9VTj0XHZPG/B Jxn88YxTu3be5VE4xFGcF8JKQBERBAHBq2Xd+RI69/JimU8cU9o7IHj2wu09UEOkt+/JefiMTJsZs /uKzF4ftMq60JvnZOD2c7VJYJoaVahc6zkw2d1R3yExSmZMYUTNX7KsJlkCRj4EAozi7/YQdTuegF 8sQPZPde+yKWyHCKNr4mseHaQvO8F7OLi+f5YeA8IiA9OrFM0tjW8QK8IaHfI2PZU5qHfWUwzS6Of DgAoIIsAYqWq1TcHc3LCLlWUb5GOHrCTVod2+qH44LwNY2QiFWu8Qo46HHBfSZ3Md+HNVfQumQdYO T+FqP+8SphoBmdKSHNbg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fFzaV-0007Qy-JV; Tue, 08 May 2018 10:11:15 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fFzaR-0007OW-Fh for linux-arm-kernel@bombadil.infradead.org; Tue, 08 May 2018 10:11:11 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=OMR/Uj8dnkBTTqYYghuUQRgpUfmVqSt5Os2RyoGJPJM=; b=YPI415RTiJJcmpCxrpJIlmSEzA qrtiBaLt27wxdbTcy9Nm2GooLlHFMgCIQ+jIT9YuCIEQXn3HNzHMTzw0Tz+ldyithaz/WAd3uXgZY scMraDBACqMvsH1q+xgO9ScNQkCNac7edWTcPOta1nkcjHmPitR8t4pGwB3oYE++VplMc9ApOMqdd D51B9Q5NpBKR5C2NoxrcXi4ajkZZB/ywK3R9nEOBfFKDqSiDqmu71NGD2zuNOVBvHQko4D2QxgWdo Z+5lRoAXWLxLvx4o+/yU/bHBDYOvosEeoZOwd8km8JagCqrXYaz1W0exoDDyzCcCLcBcVKGc//RL/ QeoYsT8Q==; Received: from metis.ext.pengutronix.de ([2001:67c:670:201:290:27ff:fe1d:cc33]) by casper.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fFzVg-0000s6-Vq for linux-arm-kernel@lists.infradead.org; Tue, 08 May 2018 10:06:18 +0000 Received: from pty.hi.pengutronix.de ([2001:67c:670:100:1d::c5]) by metis.ext.pengutronix.de with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1fFzVF-0001wZ-Fc; Tue, 08 May 2018 12:05:49 +0200 Received: from ukl by pty.hi.pengutronix.de with local (Exim 4.89) (envelope-from ) id 1fFzVB-0004FY-0h; Tue, 08 May 2018 12:05:45 +0200 From: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= To: Greg Kroah-Hartman , Jiri Slaby , Johan Hovold , Jacek Anaszewski , Pavel Machek Subject: [PATCH v3 1/3] leds: triggers: provide led_trigger_register_format() Date: Tue, 8 May 2018 12:05:41 +0200 Message-Id: <20180508100543.12559-2-u.kleine-koenig@pengutronix.de> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180508100543.12559-1-u.kleine-koenig@pengutronix.de> References: <20180508100543.12559-1-u.kleine-koenig@pengutronix.de> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::c5 X-SA-Exim-Mail-From: ukl@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-arm-kernel@lists.infradead.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180508_110617_057033_21998D37 X-CRM114-Status: GOOD ( 23.62 ) X-Spam-Score: -0.0 (/) X-Spam-Report: SpamAssassin version 3.4.1 on casper.infradead.org summary: Content analysis details: (-0.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2001:67c:670:201:290:27ff:fe1d:cc33 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: One Thousand Gnomes , Florian Fainelli , linux-serial@vger.kernel.org, Mathieu Poirier , linux-kernel@vger.kernel.org, linux-can@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kernel@pengutronix.de, Robin Murphy , linux-leds@vger.kernel.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+incoming-imx=patchwork.ozlabs.org@lists.infradead.org List-Id: linux-imx-kernel.lists.patchwork.ozlabs.org This allows one to simplify drivers that provide a trigger with a non-constant name (e.g. one trigger per device with the trigger name depending on the device's name). Internally the memory the name member of struct led_trigger points to now always allocated dynamically instead of just taken from the caller. The function led_trigger_rename_static() must be changed accordingly and was renamed to led_trigger_rename() for consistency, with the only user adapted. Signed-off-by: Uwe Kleine-König --- drivers/leds/led-triggers.c | 84 +++++++++++++++++++++++++++---------- drivers/net/can/led.c | 6 +-- include/linux/leds.h | 30 +++++++------ 3 files changed, 81 insertions(+), 39 deletions(-) diff --git a/drivers/leds/led-triggers.c b/drivers/leds/led-triggers.c index 431123b048a2..5d8bb504b07b 100644 --- a/drivers/leds/led-triggers.c +++ b/drivers/leds/led-triggers.c @@ -175,18 +175,34 @@ void led_trigger_set_default(struct led_classdev *led_cdev) } EXPORT_SYMBOL_GPL(led_trigger_set_default); -void led_trigger_rename_static(const char *name, struct led_trigger *trig) +int led_trigger_rename(struct led_trigger *trig, const char *fmt, ...) { - /* new name must be on a temporary string to prevent races */ - BUG_ON(name == trig->name); + const char *prevname; + const char *newname; + va_list args; + + if (!trig) + return 0; + + va_start(args, fmt); + newname = kvasprintf_const(GFP_KERNEL, fmt, args); + va_end(args); + + if (!newname) { + pr_err("Failed to allocate new name for trigger %s\n", trig->name); + return -ENOMEM; + } down_write(&triggers_list_lock); - /* this assumes that trig->name was originaly allocated to - * non constant storage */ - strcpy((char *)trig->name, name); + prevname = trig->name; + trig->name = newname; up_write(&triggers_list_lock); + + kfree_const(prevname); + + return 0; } -EXPORT_SYMBOL_GPL(led_trigger_rename_static); +EXPORT_SYMBOL_GPL(led_trigger_rename); /* LED Trigger Interface */ @@ -333,34 +349,56 @@ void led_trigger_blink_oneshot(struct led_trigger *trig, } EXPORT_SYMBOL_GPL(led_trigger_blink_oneshot); -void led_trigger_register_simple(const char *name, struct led_trigger **tp) +int led_trigger_register_format(struct led_trigger **tp, const char *fmt, ...) { + va_list args; struct led_trigger *trig; - int err; + int err = -ENOMEM; + const char *name; + + va_start(args, fmt); + name = kvasprintf_const(GFP_KERNEL, fmt, args); + va_end(args); trig = kzalloc(sizeof(struct led_trigger), GFP_KERNEL); - if (trig) { - trig->name = name; - err = led_trigger_register(trig); - if (err < 0) { - kfree(trig); - trig = NULL; - pr_warn("LED trigger %s failed to register (%d)\n", - name, err); - } - } else { - pr_warn("LED trigger %s failed to register (no memory)\n", - name); - } + if (!name || !trig) + goto err; + + trig->name = name; + + err = led_trigger_register(trig); + if (err < 0) + goto err; + *tp = trig; + + return 0; + +err: + kfree(trig); + kfree_const(name); + + *tp = NULL; + + return err; +} +EXPORT_SYMBOL_GPL(led_trigger_register_format); + +void led_trigger_register_simple(const char *name, struct led_trigger **tp) +{ + int ret = led_trigger_register_format(tp, "%s", name); + if (ret < 0) + pr_warn("LED trigger %s failed to register (%d)\n", name, ret); } EXPORT_SYMBOL_GPL(led_trigger_register_simple); void led_trigger_unregister_simple(struct led_trigger *trig) { - if (trig) + if (trig) { led_trigger_unregister(trig); + kfree_const(trig->name); + } kfree(trig); } EXPORT_SYMBOL_GPL(led_trigger_unregister_simple); diff --git a/drivers/net/can/led.c b/drivers/net/can/led.c index c1b667675fa1..2d7d1b0d20f9 100644 --- a/drivers/net/can/led.c +++ b/drivers/net/can/led.c @@ -115,13 +115,13 @@ static int can_led_notifier(struct notifier_block *nb, unsigned long msg, if (msg == NETDEV_CHANGENAME) { snprintf(name, sizeof(name), "%s-tx", netdev->name); - led_trigger_rename_static(name, priv->tx_led_trig); + led_trigger_rename(priv->tx_led_trig, name); snprintf(name, sizeof(name), "%s-rx", netdev->name); - led_trigger_rename_static(name, priv->rx_led_trig); + led_trigger_rename(priv->rx_led_trig, name); snprintf(name, sizeof(name), "%s-rxtx", netdev->name); - led_trigger_rename_static(name, priv->rxtx_led_trig); + led_trigger_rename(priv->rxtx_led_trig, name); } return NOTIFY_DONE; diff --git a/include/linux/leds.h b/include/linux/leds.h index b7e82550e655..e706c28bb35b 100644 --- a/include/linux/leds.h +++ b/include/linux/leds.h @@ -275,6 +275,8 @@ extern void led_trigger_unregister(struct led_trigger *trigger); extern int devm_led_trigger_register(struct device *dev, struct led_trigger *trigger); +extern int led_trigger_register_format(struct led_trigger **trigger, + const char *fmt, ...); extern void led_trigger_register_simple(const char *name, struct led_trigger **trigger); extern void led_trigger_unregister_simple(struct led_trigger *trigger); @@ -298,28 +300,25 @@ static inline void *led_get_trigger_data(struct led_classdev *led_cdev) } /** - * led_trigger_rename_static - rename a trigger - * @name: the new trigger name + * led_trigger_rename - rename a trigger * @trig: the LED trigger to rename + * @fmt: format string for new name * - * Change a LED trigger name by copying the string passed in - * name into current trigger name, which MUST be large - * enough for the new string. - * - * Note that name must NOT point to the same string used - * during LED registration, as that could lead to races. - * - * This is meant to be used on triggers with statically - * allocated name. + * rebaptize the given trigger. */ -extern void led_trigger_rename_static(const char *name, - struct led_trigger *trig); +extern int led_trigger_rename(struct led_trigger *trig, const char *fmt, ...); #else /* Trigger has no members */ struct led_trigger {}; +static inline int led_trigger_register_format(struct led_trigger **trigger, + const char *fmt, ...) +{ + return 0; +} + /* Trigger inline empty functions */ static inline void led_trigger_register_simple(const char *name, struct led_trigger **trigger) {} @@ -342,6 +341,11 @@ static inline void *led_get_trigger_data(struct led_classdev *led_cdev) return NULL; } +static inline int led_trigger_rename(struct led_trigger *trig, const char *fmt, ...) +{ + return 0; +} + #endif /* CONFIG_LEDS_TRIGGERS */ /* Trigger specific functions */