public inbox for linux-hwmon@vger.kernel.org
 help / color / mirror / Atom feed
From: Lucas Magasweran <lucasrangit@gmail.com>
To: linux-hwmon@vger.kernel.org, jdelvare@suse.de
Subject: hwmon_device_register_with_info registration API issue
Date: Sat, 05 May 2018 00:22:29 +0200	[thread overview]
Message-ID: <1525472549.6877.81.camel@gmail.com> (raw)

Hi Jean and al,

Thank you for your Kernel Recipes 2016 talk about the new hwmon
registration API [1].

I'm trying to use the latest hwmon_device_register_with_info() API
where hwmon core handles the sysfs attributes for me. However, I cannot
use it with a NULL parent struct device *dev and non-NULL struct
hwmon_chip_info *chip. My platform driver module_init() calls
hwmon_device_register_with_info(). __hwmon_device_register() has a NULL
pointer dereference error because it uses device managed memory
allocation internally.

For example,

my_hwmon_dev = hwmon_device_register_with_info(NULL, "my_name", NULL,
                                               &my_hwmon_chip_info,
                                               NULL);
--> __hwmon_device_register(NULL, "my_name", NULL,
                            &my_hwmon_chip_info, NULL);
----> hwdev->groups = devm_kcalloc(NULL, ngroups,
                                   sizeof(*groups), GFP_KERNEL);

I see that later in the function dev is checked for NULL in "hdev-
>of_node = dev ? dev->of_node : NULL;" but it cannot be NULL for
devm_kcalloc().

I tried the following 4.10.17 patch because I expected the hwmon
attributes to be stored with the hwmon device directly and not the
struct device *dev parent. However, none of my attributes are showing
up in /sys/class/hwmon#/.

Is my approach wrong?

diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index 3932f92..2e32589 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -560,6 +560,17 @@ __hwmon_device_register(struct device *dev, const
char *name, void *drvdata,
 
        hdev = &hwdev->dev;
 
+       hwdev->name = name;
+       hdev->class = &hwmon_class;
+       hdev->parent = dev;
+       hdev->of_node = dev ? dev->of_node : NULL;
+       hwdev->chip = chip;
+       dev_set_drvdata(hdev, drvdata);
+       dev_set_name(hdev, HWMON_ID_FORMAT, id);
+       err = device_register(hdev);
+       if (err)
+               goto free_hwmon;
+
        if (chip) {
                struct attribute **attrs;
                int ngroups = 2; /* terminating NULL plus &hwdev-
>groups */
@@ -568,14 +579,14 @@ __hwmon_device_register(struct device *dev, const
char *name, void *drvdata,
                        for (i = 0; groups[i]; i++)
                                ngroups++;
 
-               hwdev->groups = devm_kcalloc(dev, ngroups,
sizeof(*groups),
+               hwdev->groups = devm_kcalloc(dev ? dev : hdev, ngroups,
sizeof(*groups),
                                             GFP_KERNEL);
                if (!hwdev->groups) {
                        err = -ENOMEM;
                        goto free_hwmon;
                }
 
-               attrs = __hwmon_create_attrs(dev, drvdata, chip);
+               attrs = __hwmon_create_attrs(dev ? dev : hdev, drvdata,
chip);
                if (IS_ERR(attrs)) {
                        err = PTR_ERR(attrs);
                        goto free_hwmon;
@@ -595,17 +606,6 @@ __hwmon_device_register(struct device *dev, const
char *name, void *drvdata,
                hdev->groups = groups;
        }
 
-       hwdev->name = name;
-       hdev->class = &hwmon_class;
-       hdev->parent = dev;
-       hdev->of_node = dev ? dev->of_node : NULL;
-       hwdev->chip = chip;
-       dev_set_drvdata(hdev, drvdata);
-       dev_set_name(hdev, HWMON_ID_FORMAT, id);
-       err = device_register(hdev);
-       if (err)
-               goto free_hwmon;
-
        if (chip && chip->ops->read &&
            chip->info[0]->type == hwmon_chip &&
            (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {


 [1] https://kernel-recipes.org/en/2016/talks/new-hwmon-device-registra
tion-api/

             reply	other threads:[~2018-05-04 22:22 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-04 22:22 Lucas Magasweran [this message]
2018-05-04 22:39 ` hwmon_device_register_with_info registration API issue Guenter Roeck
2018-05-08 11:43   ` [PATCH] hwmon: (core) check parent dev != NULL when chip != NULL Lucas Magasweran
2018-05-08 11:43     ` Lucas Magasweran
2018-05-08 22:16       ` Guenter Roeck

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=1525472549.6877.81.camel@gmail.com \
    --to=lucasrangit@gmail.com \
    --cc=jdelvare@suse.de \
    --cc=linux-hwmon@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox