All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Rafael J. Wysocki" <rafael@kernel.org>
To: Qibo Huang <huangqibo.tech@gmail.com>
Cc: rafael@kernel.org, daniel.lezcano@linaro.org, amitk@kernel.org,
	rui.zhang@intel.com, linux-pm@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] thermal/core: cooling device duplicate creation check
Date: Fri, 28 Oct 2022 19:42:51 +0200	[thread overview]
Message-ID: <CAJZ5v0gFTmJY0TzYkPeEWeeUVz+zMccnYEXHyvk-mV2ypWUwRg@mail.gmail.com> (raw)
In-Reply-To: <20221027031648.2452-1-huangqibo.tech@gmail.com>

On Thu, Oct 27, 2022 at 5:17 AM Qibo Huang <huangqibo.tech@gmail.com> wrote:
>
> Because creating a cooling device may have duplicate names.

Why is this a problem?

> When creating, first check thermal_cdev_list whether
> there is a device with the same name. If it has the same name,
> it returns a reference to the cooling device.

Why is this a correct and the best possible solution?

> Signed-off-by: Qibo Huang <huangqibo.tech@gmail.com>
> ---
>  drivers/thermal/thermal_core.c | 34 ++++++++++++++++++++++++++++++++++
>  1 file changed, 34 insertions(+)
>
> diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
> index 7e669b60a065..f38f9464e9f4 100644
> --- a/drivers/thermal/thermal_core.c
> +++ b/drivers/thermal/thermal_core.c
> @@ -844,6 +844,34 @@ static void bind_cdev(struct thermal_cooling_device *cdev)
>         mutex_unlock(&thermal_list_lock);
>  }
>
> +struct thermal_cooling_device *thermal_cdev_get_zone_by_name(const char *name)
> +{
> +       struct thermal_cooling_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
> +       unsigned int found = 0;
> +
> +       if (!name)
> +               goto exit;
> +
> +       mutex_lock(&thermal_list_lock);
> +       list_for_each_entry(pos, &thermal_cdev_list, node)
> +               if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
> +                       found++;
> +                       ref = pos;
> +               }
> +       mutex_unlock(&thermal_list_lock);
> +
> +       /* nothing has been found, thus an error code for it */
> +       if (found == 0)
> +               ref = ERR_PTR(-ENODEV);
> +       else if (found > 1)
> +       /* Success only when an unique zone is found */
> +               ref = ERR_PTR(-EEXIST);
> +
> +exit:
> +       return ref;
> +}
> +EXPORT_SYMBOL_GPL(thermal_cdev_get_zone_by_name);
> +
>  /**
>   * __thermal_cooling_device_register() - register a new thermal cooling device
>   * @np:                a pointer to a device tree node.
> @@ -873,6 +901,12 @@ __thermal_cooling_device_register(struct device_node *np,
>             !ops->set_cur_state)
>                 return ERR_PTR(-EINVAL);
>
> +       if (type) {
> +               cdev = thermal_cdev_get_zone_by_name(type);
> +               if (!IS_ERR(cdev))
> +                       return cdev;
> +       }
> +
>         cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
>         if (!cdev)
>                 return ERR_PTR(-ENOMEM);
> --
> 2.37.1
>

  parent reply	other threads:[~2022-10-28 17:43 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-27  3:16 [PATCH] thermal/core: cooling device duplicate creation check Qibo Huang
2022-10-27 11:01 ` kernel test robot
2022-10-27 11:12 ` kernel test robot
2022-10-28 17:42 ` Rafael J. Wysocki [this message]
  -- strict thread matches above, loose matches on Subject: below --
2022-10-26 12:03 Qibo Huang
2022-10-26 15:58 ` kernel test robot
2022-10-26 19:10 ` kernel test robot
2022-10-26 21:32 ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAJZ5v0gFTmJY0TzYkPeEWeeUVz+zMccnYEXHyvk-mV2ypWUwRg@mail.gmail.com \
    --to=rafael@kernel.org \
    --cc=amitk@kernel.org \
    --cc=daniel.lezcano@linaro.org \
    --cc=huangqibo.tech@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=rui.zhang@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.