some questions about container_of and user_struct
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,
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@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-- Regards, Peter Teoh
participants (2)
-
loody -
Peter Teoh