kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* container_of
@ 2015-01-17 16:32 Simon Brand
  2015-01-17 16:58 ` container_of Manish Katiyar
  2015-01-17 19:13 ` container_of Anish Kumar
  0 siblings, 2 replies; 6+ messages in thread
From: Simon Brand @ 2015-01-17 16:32 UTC (permalink / raw)
  To: kernelnewbies

Good evening,

i read the article about the container_of function:
http://www.kroah.com/log/linux/container_of.html

 I understand what it does and how it works.

I thought, it could be simplified by replacing it through this:

#define container_of(ptr, type, member) ({ \
                (type *)( (char *)ptr - offsetof(type,member) );})

Original:
#define container_of(ptr, type, member) ({ \
                const typeof( ((type *)0)->member ) *__mptr = (ptr); 
                (type *)( (char *)__mptr - offsetof(type,member) );})


ptr has the type of a pointer to the member, which should be the same
as __mptr? The value should although be the same.

First I tried it in a self written script, then replaced it in
include/linux/kernel.h and compiled it as usermode linux -> working
well.

Then I compiled it and run it in a VM, but it is not working.

Can you please explain to me, why the original version is always working
and "mine" is not? 

Thank you for your time!

Regards,
Simon

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

* container_of
  2015-01-17 16:32 container_of Simon Brand
@ 2015-01-17 16:58 ` Manish Katiyar
  2015-01-17 19:45   ` container_of Simon Brand
  2015-01-17 19:13 ` container_of Anish Kumar
  1 sibling, 1 reply; 6+ messages in thread
From: Manish Katiyar @ 2015-01-17 16:58 UTC (permalink / raw)
  To: kernelnewbies

on Sat, Jan 17, 2015 at 8:32 AM, Simon Brand <simon.brand@postadigitale.de>
wrote:

> Good evening,
>
> i read the article about the container_of function:
> http://www.kroah.com/log/linux/container_of.html
>
>  I understand what it does and how it works.
>
> I thought, it could be simplified by replacing it through this:
>
> #define container_of(ptr, type, member) ({ \
>                 (type *)( (char *)ptr - offsetof(type,member) );})
>
> Original:
> #define container_of(ptr, type, member) ({ \
>                 const typeof( ((type *)0)->member ) *__mptr = (ptr);
>                 (type *)( (char *)__mptr - offsetof(type,member) );})
>
>
> ptr has the type of a pointer to the member, which should be the same
> as __mptr? The value should although be the same.
>
> First I tried it in a self written script, then replaced it in
> include/linux/kernel.h and compiled it as usermode linux -> working
> well.
>
> Then I compiled it and run it in a VM, but it is not working.
>
> Can you please explain to me, why the original version is always working
> and "mine" is not?
>


Have you searched through archives. Exactly 7 years ago, I had the same
question.

http://comments.gmane.org/gmane.linux.kernel.kernelnewbies/24141

Thanks -
Manish




> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150117/d27a94a6/attachment.html 

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

* container_of
  2015-01-17 16:32 container_of Simon Brand
  2015-01-17 16:58 ` container_of Manish Katiyar
@ 2015-01-17 19:13 ` Anish Kumar
  1 sibling, 0 replies; 6+ messages in thread
From: Anish Kumar @ 2015-01-17 19:13 UTC (permalink / raw)
  To: kernelnewbies





> On Jan 17, 2015, at 8:32 AM, Simon Brand <simon.brand@postadigitale.de> wrote:
> 
> Good evening,
> 
> i read the article about the container_of function:
> http://www.kroah.com/log/linux/container_of.html
> 
> I understand what it does and how it works.
> 
> I thought, it could be simplified by replacing it through this:
> 
> #define container_of(ptr, type, member) ({ \
>                (type *)( (char *)ptr - offsetof(type,member) );})
> 
> Original:
> #define container_of(ptr, type, member) ({ \
>                const typeof( ((type *)0)->member ) *__mptr = (ptr); 
>                (type *)( (char *)__mptr - offsetof(type,member) );})
> 
> 
> ptr has the type of a pointer to the member, which should be the same
> as __mptr? The value should although be the same.
> 
> First I tried it in a self written script, then replaced it in
> include/linux/kernel.h and compiled it as usermode linux -> working
> well.
> 
> Then I compiled it and run it in a VM, but it is not working.
What do you mean by that? What is not working?
> 
> Can you please explain to me, why the original version is always working
> and "mine" is not? 
> 
> Thank you for your time!
> 
> Regards,
> Simon
> 
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

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

* container_of
  2015-01-17 16:58 ` container_of Manish Katiyar
@ 2015-01-17 19:45   ` Simon Brand
  2015-01-17 19:53     ` container_of Mike Krinkin
  0 siblings, 1 reply; 6+ messages in thread
From: Simon Brand @ 2015-01-17 19:45 UTC (permalink / raw)
  To: kernelnewbies

Am Sat, 17 Jan 2015 08:58:13 -0800
schrieb Manish Katiyar <mkatiyar@gmail.com>:

> Have you searched through archives. Exactly 7 years ago, I had the
> same question.
> 
> http://comments.gmane.org/gmane.linux.kernel.kernelnewbies/24141
> 

No, sorry, I missed that :-x
Thank you.

To get this straight: it is only to produce a warning at compile time,
when it is misused?


I compiled the kernel two times, one time with the original code and
one time with
#define container_of(ptr, type, member) ({			\
	(type *)( (char *)ptr - offsetof(type,member) );})


The secound kernel does not work proberly. 
First there is a kernel BUG at include/drm/drm_mm.h:145 at every boot:
http://sprunge.us/MdDa

Secound the kernel hangs on reboot and poweroff:
reboot:
http://picpaste.de/pics/8a041c11f3f5e24faebc1abb41b1db3f.1421523345.png
poweroff:
http://picpaste.de/pics/b1ab5225f37572a31b43e2fb8526e890.1421523472.png

Third for example startx only produces the output:
waiting for X server to begin accepting connections

There is no further output in dmesg.

The X server starts correctly with the first/original kernel.

I compiled both kernels with following config:
https://projects.archlinux.org/svntogit/packages.git/plain/trunk/config.x86_64?h=packages/linux
and following patches:
https://projects.archlinux.org/svntogit/packages.git/plain/trunk/0001-drm-i915-Disallow-pin-ioctl-completely-for-kms-drive.patch?h=packages/linux
https://projects.archlinux.org/svntogit/packages.git/plain/trunk/change-default-console-loglevel.patch?h=packages/linux


I compiled a little c code with both defines and gcc is producing
another binary, but both are working as they should.


Thank you for your reply.
Simon

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

* container_of
  2015-01-17 19:45   ` container_of Simon Brand
@ 2015-01-17 19:53     ` Mike Krinkin
  2015-01-19 15:18       ` container_of Simon Brand
  0 siblings, 1 reply; 6+ messages in thread
From: Mike Krinkin @ 2015-01-17 19:53 UTC (permalink / raw)
  To: kernelnewbies

Hi, Simon

> I compiled the kernel two times, one time with the original code and
> one time with
> #define container_of(ptr, type, member) ({			\
> 	(type *)( (char *)ptr - offsetof(type,member) );})

try with following version:

#define container_of(ptr, type, member) ({ \
	(type *)((char *)(ptr) - offsetof(type, member));})

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

* container_of
  2015-01-17 19:53     ` container_of Mike Krinkin
@ 2015-01-19 15:18       ` Simon Brand
  0 siblings, 0 replies; 6+ messages in thread
From: Simon Brand @ 2015-01-19 15:18 UTC (permalink / raw)
  To: kernelnewbies

Am Sat, 17 Jan 2015 22:53:10 +0300
schrieb Mike Krinkin <krinkin.m.u@gmail.com>:

> 
> #define container_of(ptr, type, member) ({ \
> 	(type *)((char *)(ptr) - offsetof(type, member));})
> 

Thank you Mike, this is working!

ptr is not evaluated before the text is replaced -.-
I should know this...

Thank you very much!

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

end of thread, other threads:[~2015-01-19 15:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-01-17 16:32 container_of Simon Brand
2015-01-17 16:58 ` container_of Manish Katiyar
2015-01-17 19:45   ` container_of Simon Brand
2015-01-17 19:53     ` container_of Mike Krinkin
2015-01-19 15:18       ` container_of Simon Brand
2015-01-17 19:13 ` container_of Anish Kumar

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).