kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* issues dealing with kobjects
@ 2014-06-20  6:01 Raghavendra
  2014-06-20  6:22 ` enjoy mindful
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Raghavendra @ 2014-06-20  6:01 UTC (permalink / raw)
  To: kernelnewbies

Hello all,

I am facing a small issue dealing with kobjects.
I am writing a simple i2c driver for which I would like to export a few 
sysfs attributes(files).
The files are many, so I've decided to pack them into a directory in 
sysfs (inside the i2c device) and so I thought of kobjects.

My private data struture is something like this :
struct my_private {
     struct i2c_client *client;
     ...
     struct kobject kobj;
};

In my probe function, I am doing something like this :
int my_probe(struct i2c_client *client, ...)
{
     struct my_private *dev;

     dev = devm_kzalloc(...);
     pr_info("%x", dev);                    /* The address that I got is 
: 0xdbf94210 */

     ....

     /* Init. and add kboject */
     kobject_init(&dev->kobj, client->dev.kobj.ktype);
     kobject_add(&dev->kobj, &client->dev.kobj, "my_dir");

     /* Export sysfs group */
     sysfs_create_group(&dev->kobj, &my_attr_grp);

     ....
}

My show function for one of the attribute is something like this :
ssize_t show(struct kobject *kobj, ... )
{
     struct my_private *dev = container_of(kobj, struct my_private, kobj);
     pr_info("%x", dev);                    /* The address that I got is 
: 0xdbf94208 */

     ....
}

I tried to probe and remove the device mutilple times. Every time I am 
getting a difference of 2bytes for the 'dev'
pointer between probe and show functions.

Can anyone explain me where am I going wrong or is there any better way 
to create directories in sysfs?
I am building this module against 3.13.2 kernel.

Thank you.
Raghavendra.



-------------------------------------------------------------------------------------------------------------------------------
[ C-DAC is on Social-Media too. Kindly follow us at:
Facebook: https://www.facebook.com/CDACINDIA & Twitter: @cdacindia ]

This e-mail is for the sole use of the intended recipient(s) and may
contain confidential and privileged information. If you are not the
intended recipient, please contact the sender by reply e-mail and destroy
all copies and the original message. Any unauthorized review, use,
disclosure, dissemination, forwarding, printing or copying of this email
is strictly prohibited and appropriate legal action will be taken.
-------------------------------------------------------------------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 4+ messages in thread

* issues dealing with kobjects
  2014-06-20  6:01 issues dealing with kobjects Raghavendra
@ 2014-06-20  6:22 ` enjoy mindful
  2014-06-20  8:24 ` Raghavendra
  2014-06-20 16:16 ` Greg KH
  2 siblings, 0 replies; 4+ messages in thread
From: enjoy mindful @ 2014-06-20  6:22 UTC (permalink / raw)
  To: kernelnewbies

You have two *different* local pointer with same name.

On Fri, Jun 20, 2014 at 2:01 PM, Raghavendra <arrao@cdac.in> wrote:
> Hello all,
>
> I am facing a small issue dealing with kobjects.
> I am writing a simple i2c driver for which I would like to export a few
> sysfs attributes(files).
> The files are many, so I've decided to pack them into a directory in
> sysfs (inside the i2c device) and so I thought of kobjects.
>
> My private data struture is something like this :
> struct my_private {
>      struct i2c_client *client;
>      ...
>      struct kobject kobj;
> };
>
> In my probe function, I am doing something like this :
> int my_probe(struct i2c_client *client, ...)
> {
>      struct my_private *dev;
>
>      dev = devm_kzalloc(...);
>      pr_info("%x", dev);                    /* The address that I got is
> : 0xdbf94210 */
>
>      ....
>
>      /* Init. and add kboject */
>      kobject_init(&dev->kobj, client->dev.kobj.ktype);
>      kobject_add(&dev->kobj, &client->dev.kobj, "my_dir");
>
>      /* Export sysfs group */
>      sysfs_create_group(&dev->kobj, &my_attr_grp);
>
>      ....
> }
>
> My show function for one of the attribute is something like this :
> ssize_t show(struct kobject *kobj, ... )
> {
>      struct my_private *dev = container_of(kobj, struct my_private, kobj);
>      pr_info("%x", dev);                    /* The address that I got is
> : 0xdbf94208 */
>
>      ....
> }
>
> I tried to probe and remove the device mutilple times. Every time I am
> getting a difference of 2bytes for the 'dev'
> pointer between probe and show functions.
>
> Can anyone explain me where am I going wrong or is there any better way
> to create directories in sysfs?
> I am building this module against 3.13.2 kernel.
>
> Thank you.
> Raghavendra.
>
>
>
> -------------------------------------------------------------------------------------------------------------------------------
> [ C-DAC is on Social-Media too. Kindly follow us at:
> Facebook: https://www.facebook.com/CDACINDIA & Twitter: @cdacindia ]
>
> This e-mail is for the sole use of the intended recipient(s) and may
> contain confidential and privileged information. If you are not the
> intended recipient, please contact the sender by reply e-mail and destroy
> all copies and the original message. Any unauthorized review, use,
> disclosure, dissemination, forwarding, printing or copying of this email
> is strictly prohibited and appropriate legal action will be taken.
> -------------------------------------------------------------------------------------------------------------------------------
>
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

* issues dealing with kobjects
  2014-06-20  6:01 issues dealing with kobjects Raghavendra
  2014-06-20  6:22 ` enjoy mindful
@ 2014-06-20  8:24 ` Raghavendra
  2014-06-20 16:16 ` Greg KH
  2 siblings, 0 replies; 4+ messages in thread
From: Raghavendra @ 2014-06-20  8:24 UTC (permalink / raw)
  To: kernelnewbies

On Friday 20 June 2014 11:31 AM, Raghavendra wrote:
> Hello all,
>
> I am facing a small issue dealing with kobjects.
> I am writing a simple i2c driver for which I would like to export a few
> sysfs attributes(files).
> The files are many, so I've decided to pack them into a directory in
> sysfs (inside the i2c device) and so I thought of kobjects.
>
> My private data struture is something like this :
> struct my_private {
>       struct i2c_client *client;
>       ...
>       struct kobject kobj;
> };
>
> In my probe function, I am doing something like this :
> int my_probe(struct i2c_client *client, ...)
> {
>       struct my_private *dev;
>
>       dev = devm_kzalloc(...);
>       pr_info("%x", dev);                    /* The address that I got is
> : 0xdbf94210 */
>
>       ....
>
>       /* Init. and add kboject */
>       kobject_init(&dev->kobj, client->dev.kobj.ktype);
>       kobject_add(&dev->kobj, &client->dev.kobj, "my_dir");
>
>       /* Export sysfs group */
>       sysfs_create_group(&dev->kobj, &my_attr_grp);
>
>       ....
> }
>
> My show function for one of the attribute is something like this :
> ssize_t show(struct kobject *kobj, ... )
> {
>       struct my_private *dev = container_of(kobj, struct my_private, kobj);
>       pr_info("%x", dev);                    /* The address that I got is
> : 0xdbf94208 */
>
>       ....
> }
>
> I tried to probe and remove the device mutilple times. Every time I am
> getting a difference of 2bytes for the 'dev'
> pointer between probe and show functions.
>
> Can anyone explain me where am I going wrong or is there any better way
> to create directories in sysfs?
> I am building this module against 3.13.2 kernel.
>
> Thank you.
> Raghavendra.
>
>
>
> -------------------------------------------------------------------------------------------------------------------------------
> [ C-DAC is on Social-Media too. Kindly follow us at:
> Facebook: https://www.facebook.com/CDACINDIA & Twitter: @cdacindia ]
>
> This e-mail is for the sole use of the intended recipient(s) and may
> contain confidential and privileged information. If you are not the
> intended recipient, please contact the sender by reply e-mail and destroy
> all copies and the original message. Any unauthorized review, use,
> disclosure, dissemination, forwarding, printing or copying of this email
> is strictly prohibited and appropriate legal action will be taken.
> -------------------------------------------------------------------------------------------------------------------------------
>
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
I guess I've solved the issue. Now the code looks something like this :

struct my_private {
      struct i2c_client *client;
      ...
      struct kobject *kobj;	/* Converted variable to ptr */
};

int my_probe(struct i2c_client *client, ...)
{
      struct my_private *dev;

      dev = devm_kzalloc(...);
  
      ....
	
/* Replaced with kobject_init and kobject_add calls */
  dev->kobj = kobject_create_and_add("my_dir", &client->dev.kobj);	

      /* Export sysfs group */
      sysfs_create_group(dev->kobj, &my_attr_grp);

      ....
}

ssize_t show(struct kobject *kobj, ... )
{
	struct device *i2cdev = kobj_to_dev(kobj->parent);
	struct i2c_client *client = to_i2c_client(i2cdev);
	struct my_private *dev = i2c_get_clientdata(client);

  ....
}

This approach worked, but there's a lot of redundancy in the show() function, just to obtain the pointer to the private data.
I would be glad if anyone suggested any better approach to create and manage sysfs directories and attribute groups.

Thank you,
Raghavendra

  






-------------------------------------------------------------------------------------------------------------------------------
[ C-DAC is on Social-Media too. Kindly follow us at:
Facebook: https://www.facebook.com/CDACINDIA & Twitter: @cdacindia ]

This e-mail is for the sole use of the intended recipient(s) and may
contain confidential and privileged information. If you are not the
intended recipient, please contact the sender by reply e-mail and destroy
all copies and the original message. Any unauthorized review, use,
disclosure, dissemination, forwarding, printing or copying of this email
is strictly prohibited and appropriate legal action will be taken.
-------------------------------------------------------------------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 4+ messages in thread

* issues dealing with kobjects
  2014-06-20  6:01 issues dealing with kobjects Raghavendra
  2014-06-20  6:22 ` enjoy mindful
  2014-06-20  8:24 ` Raghavendra
@ 2014-06-20 16:16 ` Greg KH
  2 siblings, 0 replies; 4+ messages in thread
From: Greg KH @ 2014-06-20 16:16 UTC (permalink / raw)
  To: kernelnewbies

On Fri, Jun 20, 2014 at 11:31:03AM +0530, Raghavendra wrote:
> Hello all,
> 
> I am facing a small issue dealing with kobjects.
> I am writing a simple i2c driver for which I would like to export a few 
> sysfs attributes(files).

Please don't, unless these attributes are something that all other i2c
drivers export and are documented in Documentation/ABI/

> The files are many, so I've decided to pack them into a directory in 
> sysfs (inside the i2c device) and so I thought of kobjects.

No, please never use "raw" kobjects in a driver, use the driver core
functions instead.  You can have an attribute group that is associated
with your driver or device, and the driver core will automatically
create the files for you (in a subdir if you want it), in a race-free
manner.  Please use that interface instead.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-06-20 16:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-20  6:01 issues dealing with kobjects Raghavendra
2014-06-20  6:22 ` enjoy mindful
2014-06-20  8:24 ` Raghavendra
2014-06-20 16:16 ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).