Received: by 10.192.165.148 with SMTP id m20csp4691387imm; Tue, 8 May 2018 12:36:48 -0700 (PDT) X-Google-Smtp-Source: AB8JxZq+IqBLccq2am0BEKJ9BFCXuVajehZY2FEeq/jlcvvMpOjGLXiZt4GLAcsrCTHxUN3Pwc0k X-Received: by 10.98.129.5 with SMTP id t5mr41000564pfd.215.1525808208168; Tue, 08 May 2018 12:36:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525808208; cv=none; d=google.com; s=arc-20160816; b=wpghs2pRaaSCw8vB+Rl+uhssYDPKZ0hP/woJkWBd4CE/OIm899B0W7iXjQakUSMSxJ pY2rl9RJ6wFBsbJpJvO6d04CE9rCaMmW0BNm/X9YKro57+wtxWxtbhHMUMGSwBYLvWfX ofqfqMexqdYDDjWNdlxDIyZEbc/rBlbyDi8PtDn0jp2VmYM0Y6vSwI6sAZDnxHod9XX4 izc77XNVL/7x8BMzL2qVJvUjZI/uZBFIl9EZ/SYMOfLZXLhuMkOIje9KItekjrG7R3/q jsMYGSgaSH5yUzJ30NKSEdsKTEINQZbV2dQt59v5F/5a/HAc/VBwyZIW82M+l0Og5GwV l9fQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature :arc-authentication-results; bh=P8j9Y+rAPty1ZgXc7EBxQwHtXCi2IBZp4ub6oKTHPX4=; b=YIMfBJL6aS9tbsvU+hikN36bUcrz0zs9278iYgLF2o7BSy79Jg52iUChWDzq3gaKmP TBqeGhj0rpLL2V/BScdmEBtjBUFiRI23gFR+icD0W70lIVwKzlIKQWvprspTZE73xucj Tr8MSWJocL9xeXzS7LE74SG7oFDQkZUq+QKzndES/boFuoMyW+lP8IxJtBkEuSr/aR9u v0zk/P/t+r05dc+bqDxIG9OnVHnu7Ajp91MHV7nlUUk3ocQLlCSHyiuuOGJpBqyfcddJ o9ug8hwXS8qAvSE75tCfCaYgBwRVCzcOkeD+fXiOA/YJBOzLUEMoQqkvh4G87YzzFFFy 08bA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=LIAe5xsj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a23si6171133pfe.364.2018.05.08.12.36.32; Tue, 08 May 2018 12:36:48 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=LIAe5xsj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755541AbeEHTek (ORCPT + 99 others); Tue, 8 May 2018 15:34:40 -0400 Received: from mail-wm0-f66.google.com ([74.125.82.66]:55611 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755437AbeEHTeh (ORCPT ); Tue, 8 May 2018 15:34:37 -0400 Received: by mail-wm0-f66.google.com with SMTP id a8so20631152wmg.5; Tue, 08 May 2018 12:34:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=P8j9Y+rAPty1ZgXc7EBxQwHtXCi2IBZp4ub6oKTHPX4=; b=LIAe5xsjM10QKa7uNKki8onHnra0XhPqQf6mK8cBr3xe3LGiwskNSxLBCGD17DWvro RdMlFWqF3bA0neeZkciwfoT+7rMA+72DnHp1EHi1tIONpIirmK5gbLckL6UKK7HFXByA uGQBmwQmYhbYT4PD+SEYyr+eabsjyxs3UhEorTc+yJwGptjkcHhOL32vHoHQtWE9v0hx DrCQKmPA7rw+b0RVRv7xiVWS4YPODCf7Gm15i8ynHu+d3LkNUZgXWDmPnlL1rMCWVMtt dgwLxOJlgYzo7Cu5dj+f3fCxDcLtK0Im+UIjBNcwoOkVPKDh4l30/wzLK5TOp2m7SeoX pf3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=P8j9Y+rAPty1ZgXc7EBxQwHtXCi2IBZp4ub6oKTHPX4=; b=smAdvx42+5WKnYjCS0zR+axUYM3YvSH8F/7FF+bcRod6hBbnyFPZjKqWabKvnTMNkv dTI6A69DD6pM0VzkWPBmeIqU1L7jo5qeHB0nEUskCV/5lq193u58QoXzXxCsXAJKcR5I GBq+x/MKyTV2suYNbFSrpbRIhT56rTNVX1a6xyeo22rCNQ3Fw7VaUNIZSBx/tdTmFmqx /6rZap14UQ50Iygb2f7uiD/jk3feFseqJJ2dmQt5VkJy97TiRah4zvVPyJjQxlB7jmSI sfqME6nvT/cGB+bK90hL3/++kketpJZbCwmDGRhNvr4aswpcHcLo0smyqFLrSClXNnuk Vpsw== X-Gm-Message-State: ALKqPwe5/okbA8RA6dasSprTQ3n/IjMp7ETm4IUpihYRL+ZFs1NgceIq RvYP0nEWZqH6ttA2Mt2gThQ= X-Received: by 2002:a1c:36e9:: with SMTP id y102-v6mr3924544wmh.152.1525808075240; Tue, 08 May 2018 12:34:35 -0700 (PDT) Received: from [192.168.1.18] (ble74.neoplus.adsl.tpnet.pl. [83.28.198.74]) by smtp.gmail.com with ESMTPSA id 67sm16465969wmw.32.2018.05.08.12.34.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 May 2018 12:34:34 -0700 (PDT) Subject: Re: [PATCH v3 1/3] leds: triggers: provide led_trigger_register_format() To: =?UTF-8?Q?Uwe_Kleine-K=c3=b6nig?= , Greg Kroah-Hartman , Jiri Slaby , Johan Hovold , Pavel Machek Cc: linux-serial@vger.kernel.org, linux-leds@vger.kernel.org, linux-can@vger.kernel.org, kernel@pengutronix.de, One Thousand Gnomes , Florian Fainelli , Mathieu Poirier , linux-kernel@vger.kernel.org, Robin Murphy , linux-arm-kernel@lists.infradead.org References: <20180508100543.12559-1-u.kleine-koenig@pengutronix.de> <20180508100543.12559-2-u.kleine-koenig@pengutronix.de> From: Jacek Anaszewski Message-ID: Date: Tue, 8 May 2018 21:33:14 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <20180508100543.12559-2-u.kleine-koenig@pengutronix.de> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Uwe, Thank you for the patch. It looks fine, but please split the drivers/net/can/led.c related changes into a separate one. Best regards, Jacek Anaszewski On 05/08/2018 12:05 PM, Uwe Kleine-König wrote: > 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 */ >