* some questions about container_of and user_struct
@ 2012-02-13 7:34 loody
2012-02-20 2:18 ` Peter Teoh
0 siblings, 1 reply; 2+ messages in thread
From: loody @ 2012-02-13 7:34 UTC (permalink / raw)
To: kernelnewbies
hi all:
I add below function in free_uid to get which task try to free user_struct
struct task_struct *p;
struct cred *c;
c = container_of(up,struct cred, user);
p = container_of(c,struct task_struct,real_cred);
printk("%s user %p, cred->user = %p p->pid = %d\n",__func__,up, c->user,p->pid);
but p->pid is always 0.
I think it is wrong for me to get cred and task by using container_of.
1. from kernel definition below, is it ok that member is pointer?
#define container_of(ptr, type, member)
2. is there already exist macros or functions I can use for #1 above?
3. what is user_struct used for? When and under what circumstances
kernel will try to release it.
4. since user_struct is allocated by kmem_cache_zalloc, is there api
or tool I can monitor it?
Thanks for your help,
^ permalink raw reply [flat|nested] 2+ messages in thread* some questions about container_of and user_struct
2012-02-13 7:34 some questions about container_of and user_struct loody
@ 2012-02-20 2:18 ` Peter Teoh
0 siblings, 0 replies; 2+ messages in thread
From: Peter Teoh @ 2012-02-20 2:18 UTC (permalink / raw)
To: kernelnewbies
On Mon, Feb 13, 2012 at 3:34 PM, loody <miloody@gmail.com> wrote:
> hi all:
> I add below function in free_uid to get which task try to free user_struct
>
> struct task_struct *p;
> struct cred *c;
> c = container_of(up,struct cred, user);
> p = container_of(c,struct task_struct,real_cred);
> printk("%s user %p, cred->user = %p p->pid = %d\n",__func__,up,
> c->user,p->pid);
>
> but p->pid is always 0.
> I think it is wrong for me to get cred and task by using container_of.
>
> 1. from kernel definition below, is it ok that member is pointer?
> #define container_of(ptr, type, member)
>
> it is just a macro, so member can be anything, and compiler will
substitute the name during preprocessing time.
> 2. is there already exist macros or functions I can use for #1 above?
>
> 3. what is user_struct used for? When and under what circumstances
> kernel will try to release it.
>
look into kernel/signal.c:__sigqueue_alloc() for example: user_struct is
pointer to a user structure for identifying the user running in a
particular process context mode, not necessarily itself.
it is free in kernel/user.c:free_user(), which is called by free_uid().
So who called free_uid()?
Look into kernel/sys.c:getpriority() syscall implementation:
} while_each_thread(g, p);
if (who != cred->uid)
free_uid(user); /* for find_user()
*/
break;
So those who called find_user() will call free_uid() (which then call
free_user()....eh...convoluted logic!!!).
See the remark in kernel/user.c:find_user():
107 /*
108 * Locate the user_struct for the passed UID. If found, take a ref
on it. The
109 * caller must undo that ref with free_uid().
110 *
111 * If the user_struct could not be found, return NULL.
112 */
113 struct user_struct *find_user(uid_t uid)
114 {
As indicated in remark above, that is the only situation I know when u have
to free the user_struct (calling free_user()).
> 4. since user_struct is allocated by kmem_cache_zalloc, is there api
> or tool I can monitor it?
>
> Thanks for your help,
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
--
Regards,
Peter Teoh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20120220/8b31d5f1/attachment.html
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2012-02-20 2:18 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-02-13 7:34 some questions about container_of and user_struct loody
2012-02-20 2:18 ` Peter Teoh
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).