Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp3201144rwl; Mon, 27 Mar 2023 10:34:08 -0700 (PDT) X-Google-Smtp-Source: AKy350YXgWFYhVshX2nlNMnun9kmZzr/lh7NWvvZD8GY71Rljr3Yy1+UQId0RcOYNsU8qBF/yUEv X-Received: by 2002:a17:903:228f:b0:19d:62b:f040 with SMTP id b15-20020a170903228f00b0019d062bf040mr16118912plh.37.1679938448148; Mon, 27 Mar 2023 10:34:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679938448; cv=none; d=google.com; s=arc-20160816; b=eo3YbprIvYD3LMnlBkoOiO7VBiv8Pt0zeEnrBXl4p1Bqep0EBho6ujkdWszUyxQna2 tl1gpfhqCXT4yaJKwcb1J0q9iIZM/9GSThMmpqIhV2+jq115YEhVc0ipix8YyDaVL4sk HTwJfe34EZToJYRasfuI+LcRuev3d5dPTF1U013+X8ilkIse44TX+F6jn3ukjItpYPbN Cyq0nwAdAHgxLGKHNg4JXgAEAY+4hFwGn/Zm8jNEgrBSQ0IfQ7pTGdsyxupUfiPfA+pY F/IW58z4taKqEta0T4iH/B+KH+geb/ffB4C1QT5Hl3VrdgmGPN1LzwXvgrkRB2nCK9qe OgKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id; bh=EUWzVxTnO12Z6f670iC4AVSZZKDIx5ttDAoovPCijW0=; b=CyJMZC9wmkYiyP27gE4L0eUxjtC2Ma4OXdf1miMHZ1GH5Ziy3wvFroE93Fchbm9ZYU CR4SyYMbFMNsH5ntCxgXy0MJv7bWuQxeubXGL27EnePcY4ipSxbTzuOAJGuGQ2FrYlX4 /Nn/bWXAAcPsIBuiei6AzCGIM1N2ZODjsAdzUm8XpOeOc3rDERc5kdErARxCWkr95xAJ RaOmjUxtpPOd/1/gBII7Pa9vrDOwD/vCYLAsSGG9VaNYE8AhOtQdH1lZwFahGMeQ4Z4a JC4LCSt8fPOhCFYZSmJjeadxen64rJkMljiiliUffW8NdfwIMYGtio+YvyuAMX19g0+n AQog== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kv14-20020a17090328ce00b0019c3d2a71c6si26904381plb.489.2023.03.27.10.33.55; Mon, 27 Mar 2023 10:34:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232642AbjC0RUX (ORCPT + 99 others); Mon, 27 Mar 2023 13:20:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232699AbjC0RUR (ORCPT ); Mon, 27 Mar 2023 13:20:17 -0400 Received: from smtp.smtpout.orange.fr (smtp-22.smtpout.orange.fr [80.12.242.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9684035A1 for ; Mon, 27 Mar 2023 10:20:06 -0700 (PDT) Received: from [192.168.1.18] ([86.243.2.178]) by smtp.orange.fr with ESMTPA id gqVmpRJB89ijugqVmpVmlV; Mon, 27 Mar 2023 19:20:04 +0200 X-ME-Helo: [192.168.1.18] X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Mon, 27 Mar 2023 19:20:04 +0200 X-ME-IP: 86.243.2.178 Message-ID: <9567ac67-0c4c-0599-7141-9137837f79e2@wanadoo.fr> Date: Mon, 27 Mar 2023 19:20:02 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Subject: Re: [PATCH v2 2/2] leds: max597x: Add support for max597x To: Naresh Solanki , Lee Jones , Pavel Machek Cc: Patrick Rudolph , linux-kernel@vger.kernel.org, linux-leds@vger.kernel.org References: <20230323194550.1914725-1-Naresh.Solanki@9elements.com> <20230323194550.1914725-2-Naresh.Solanki@9elements.com> <688423c6-ba98-002c-efe5-7b0997d6af73@9elements.com> <2077c7e1-cf69-8648-20ac-23793f92ad14@9elements.com> Content-Language: fr, en-CA From: Christophe JAILLET In-Reply-To: <2077c7e1-cf69-8648-20ac-23793f92ad14@9elements.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.0 required=5.0 tests=NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Le 27/03/2023 à 17:47, Naresh Solanki a écrit : > Hi, > > On 24-03-2023 09:06 pm, Christophe JAILLET wrote: >> Le 24/03/2023 à 11:54, Naresh Solanki a écrit : >>> Hi, >>> >>> On 24-03-2023 01:48 am, Christophe JAILLET wrote: >>>> Le 23/03/2023 à 20:45, Naresh Solanki a écrit : >>>>> From: Patrick Rudolph >>>>> >>>>> max597x is hot swap controller with indicator LED support. >>>>> This driver uses DT property to configure led during boot time & >>>>> also provide the LED control in sysfs. >>>>> >> >> [...] >> >> >>>>> +static int max597x_led_probe(struct platform_device *pdev) >>>>> +{ >>>>> +    struct device_node *np = dev_of_node(pdev->dev.parent); >>>>> +    struct regmap *regmap = dev_get_regmap(pdev->dev.parent, NULL); >>>>> +    struct device_node *led_node; >>>>> +    struct device_node *child; >>>>> +    int ret = 0; >>>>> + >>>>> +    if (!regmap) >>>>> +        return -EPROBE_DEFER; >>>>> + >>>>> +    led_node = of_get_child_by_name(np, "leds"); >>>>> +    if (!led_node) >>>>> +        return -ENODEV; >>>>> + >>>>> +    for_each_available_child_of_node(led_node, child) { >>>>> +        u32 reg; >>>>> + >>>>> +        if (of_property_read_u32(child, "reg", ®)) >>>>> +            continue; >>>>> + >>>>> +        if (reg >= MAX597X_NUM_LEDS) { >>>>> +            dev_err(&pdev->dev, "invalid LED (%u >= %d)\n", reg, >>>>> +                MAX597X_NUM_LEDS); >>>>> +            continue; >>>>> +        } >>>>> + >>>>> +        ret = max597x_setup_led(&pdev->dev, regmap, child, reg); >>>>> +        if (ret < 0) >>>>> +            of_node_put(child); >>>> >>>> This of_node_put() looks odd to me. >>> Not sure if I get this right but if led setup fails of_node_put >>> should be called. >> >> My understanding is that this of_node_put() is there in case of error, >> to release what would otherwise never be released by >> for_each_available_child_of_node() if we exit early from the loop. >> >> If the purpose is to release a reference taken in max597x_setup_led() >> in case of error: >>     - this should be done IMHO within max597x_setup_led() directly >>     - there should be a of_node_get() somewhere in max597x_setup_led() >>       (after: >>      if (of_property_read_string(nc, "label", &led->led.name)) >>          led->led.name = nc->name; >>        + error handling path,  *or* >>       just before the final return ret; when we know that everything >> is fine, >>       if I understand correctly the code) >> >> Is the reference taken elsewhere? >> Did I miss something obvious? >> >> > One of the reference is "drivers/leds/leds-sc27xx-bltc.c" line 311 > Please do let me know if I have to do anything about it. By reference, I was speaking of reference taken by a of_node_get() call and released by a of_node_put() call. Anyway, I do agree with leds-sc27xx-bltc.c. There is a of_node_put() because for_each_available_child_of_node() won't be able to do it by itself *in case of early return* ("return err;") In all other paths (when the loop goes to the end), the reference taken by for_each_available_child_of_node() is also released, on the next iteration, by for_each_available_child_of_node(). In *your* case, if you don't break or return, there is no need to call of_node_put() explicitly. It would lead to a double put. (yours and the one that will be done by for_each_available_child_of_node()). Have a look at for_each_available_child_of_node() and more specifically at of_get_next_available_child(). At the first call 'child' is NULL. A ref is taken [1]. Nothing is released. For following calls, a new ref is taken on a new node [1], and the previous reference is released [2]. On the last call, the 'for' loop will not be executed because there is nothing to scan anymore. No new reference is taken, and the previous (and last) refence is finally released [2]. [1]: https://elixir.bootlin.com/linux/v6.3-rc3/source/drivers/of/base.c#L808 [2]: https://elixir.bootlin.com/linux/v6.3-rc3/source/drivers/of/base.c#L811 > >>>> "return ret;" or "break;" missing ? >>>> >>> Didn't add a break so that it can continue initializing remaining led >>> if any. >> >> Ok. Don't know the code enough to see if correct or not, but based on >> my comment above, I think that something is missing in >> max597x_setup_led() and that errors should be silently ignored here. > In my implementation, I have chosen to continue with the next LED if an > error occurs, rather than aborting the 'for loop' with an error. I have > seen other implementations also done in a similar way. > Do you have any further inputs or suggestions on this approach. No, sorry, I won't be of any help on what design is the best. CJ