some questions about container_of and user_struct

Peter Teoh htmldeveloper at gmail.com
Sun Feb 19 21:18:10 EST 2012


On Mon, Feb 13, 2012 at 3:34 PM, loody <miloody at 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 


More information about the Kernelnewbies mailing list