2 * linux/kernel/vserver/context.c
4 * Virtual Server: Context Support
6 * Copyright (C) 2003-2004 Herbert Pƶtzl
9 * V0.02 vx_ctx_kill syscall command
10 * V0.03 replaced context_info calls
11 * V0.04 redesign of struct (de)alloc
12 * V0.05 rlimit basic implementation
13 * V0.06 task_xid and info commands
14 * V0.07 context flags and caps
15 * V0.08 switch to RCU based hash
19 #include <linux/config.h>
20 #include <linux/slab.h>
21 #include <linux/vserver/context.h>
22 #include <linux/vserver/legacy.h>
23 #include <linux/vinline.h>
24 #include <linux/kernel_stat.h>
25 #include <linux/namespace.h>
26 #include <linux/rcupdate.h>
28 #include <asm/errno.h>
33 * allocate an initialized vx_info struct
34 * doesn't make it visible (hash) */
36 static struct vx_info *__alloc_vx_info(xid_t xid)
38 struct vx_info *new = NULL;
40 vxdprintk("alloc_vx_info(%d)\n", xid);
42 /* would this benefit from a slab cache? */
43 new = kmalloc(sizeof(struct vx_info), GFP_KERNEL);
47 memset (new, 0, sizeof(struct vx_info));
49 INIT_RCU_HEAD(&new->vx_rcu);
50 INIT_HLIST_NODE(&new->vx_hlist);
51 atomic_set(&new->vx_refcnt, 0);
52 atomic_set(&new->vx_usecnt, 0);
54 /* rest of init goes here */
55 vx_info_init_limit(&new->limit);
56 vx_info_init_sched(&new->sched);
57 vx_info_init_cvirt(&new->cvirt);
58 vx_info_init_cacct(&new->cacct);
60 new->vx_flags = VXF_STATE_SETUP|VXF_STATE_INIT;
61 new->vx_bcaps = CAP_INIT_EFF_SET;
64 vxdprintk("alloc_vx_info(%d) = %p\n", xid, new);
68 /* __dealloc_vx_info()
70 * final disposal of vx_info */
72 static void __dealloc_vx_info(struct vx_info *vxi)
74 vxdprintk("dealloc_vx_info(%p)\n", vxi);
76 vxi->vx_hlist.next = LIST_POISON1;
79 if (vxi->vx_namespace)
80 put_namespace(vxi->vx_namespace);
82 put_fs_struct(vxi->vx_fs);
84 vx_info_exit_limit(&vxi->limit);
85 vx_info_exit_sched(&vxi->sched);
86 vx_info_exit_cvirt(&vxi->cvirt);
87 vx_info_exit_cacct(&vxi->cacct);
89 BUG_ON(atomic_read(&vxi->vx_usecnt));
90 BUG_ON(atomic_read(&vxi->vx_refcnt));
96 /* hash table for vx_info hash */
98 #define VX_HASH_SIZE 13
100 struct hlist_head vx_info_hash[VX_HASH_SIZE];
102 static spinlock_t vx_info_hash_lock = SPIN_LOCK_UNLOCKED;
105 static inline unsigned int __hashval(xid_t xid)
107 return (xid % VX_HASH_SIZE);
114 * add the vxi to the global hash table
115 * requires the hash_lock to be held */
117 static inline void __hash_vx_info(struct vx_info *vxi)
119 struct hlist_head *head;
121 vxdprintk("__hash_vx_info: %p[#%d]\n", vxi, vxi->vx_id);
123 head = &vx_info_hash[__hashval(vxi->vx_id)];
124 hlist_add_head_rcu(&vxi->vx_hlist, head);
127 /* __unhash_vx_info()
129 * remove the vxi from the global hash table
130 * requires the hash_lock to be held */
132 static inline void __unhash_vx_info(struct vx_info *vxi)
134 vxdprintk("__unhash_vx_info: %p[#%d]\n", vxi, vxi->vx_id);
135 hlist_del_rcu(&vxi->vx_hlist);
140 /* __lookup_vx_info()
142 * requires the rcu_read_lock()
143 * doesn't increment the vx_refcnt */
145 static inline struct vx_info *__lookup_vx_info(xid_t xid)
147 struct hlist_head *head = &vx_info_hash[__hashval(xid)];
148 struct hlist_node *pos;
150 hlist_for_each(pos, head) {
151 struct vx_info *vxi =
152 hlist_entry(pos, struct vx_info, vx_hlist);
154 if (vxi->vx_id == xid) {
164 * find unused dynamic xid
165 * requires the hash_lock to be held */
167 static inline xid_t __vx_dynamic_id(void)
169 static xid_t seq = MAX_S_CONTEXT;
173 if (++seq > MAX_S_CONTEXT)
175 if (!__lookup_vx_info(seq))
177 } while (barrier != seq);
183 * locate or create the requested context
184 * get() it and if new hash it */
186 static struct vx_info * __loc_vx_info(int id, int *err)
188 struct vx_info *new, *vxi = NULL;
190 vxdprintk("loc_vx_info(%d)\n", id);
192 if (!(new = __alloc_vx_info(id))) {
197 spin_lock(&vx_info_hash_lock);
199 /* dynamic context requested */
200 if (id == VX_DYNAMIC_ID) {
201 id = __vx_dynamic_id();
203 printk(KERN_ERR "no dynamic context available.\n");
208 /* existing context requested */
209 else if ((vxi = __lookup_vx_info(id))) {
210 /* context in setup is not available */
211 if (vxi->vx_flags & VXF_STATE_SETUP) {
212 vxdprintk("loc_vx_info(%d) = %p (not available)\n", id, vxi);
216 vxdprintk("loc_vx_info(%d) = %p (found)\n", id, vxi);
223 /* new context requested */
224 vxdprintk("loc_vx_info(%d) = %p (new)\n", id, new);
225 __hash_vx_info(get_vx_info(new));
226 vxi = new, new = NULL;
230 spin_unlock(&vx_info_hash_lock);
232 __dealloc_vx_info(new);
242 void rcu_free_vx_info(void *obj)
244 struct vx_info *vxi = obj;
247 usecnt = atomic_read(&vxi->vx_usecnt);
250 refcnt = atomic_read(&vxi->vx_refcnt);
254 __dealloc_vx_info(vxi);
256 printk("!!! rcu didn't free\n");
259 void unhash_vx_info(struct vx_info *vxi)
261 spin_lock(&vx_info_hash_lock);
262 __unhash_vx_info(vxi);
263 spin_unlock(&vx_info_hash_lock);
268 * search for a vx_info and get() it
269 * negative id means current */
271 struct vx_info *locate_vx_info(int id)
276 vxi = get_vx_info(current->vx_info);
279 vxi = get_vx_info(__lookup_vx_info(id));
285 /* vx_info_is_hashed()
287 * verify that xid is still hashed */
289 int vx_info_is_hashed(xid_t xid)
294 hashed = (__lookup_vx_info(xid) != NULL);
299 #ifdef CONFIG_VSERVER_LEGACY
302 struct vx_info *alloc_vx_info(xid_t xid)
304 return __alloc_vx_info(xid);
308 struct vx_info *locate_or_create_vx_info(int id)
312 return __loc_vx_info(id, &err);
317 #ifdef CONFIG_PROC_FS
319 #define hlist_for_each_rcu(pos, head) \
320 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1;}); \
321 pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
323 int get_xid_list(int index, unsigned int *xids, int size)
325 int hindex, nr_xids = 0;
328 for (hindex = 0; hindex < VX_HASH_SIZE; hindex++) {
329 struct hlist_head *head = &vx_info_hash[hindex];
330 struct hlist_node *pos;
332 hlist_for_each_rcu(pos, head) {
338 vxi = hlist_entry(pos, struct vx_info, vx_hlist);
339 xids[nr_xids] = vxi->vx_id;
340 if (++nr_xids >= size)
350 int vx_migrate_user(struct task_struct *p, struct vx_info *vxi)
352 struct user_struct *new_user, *old_user;
356 new_user = alloc_uid(vxi->vx_id, p->uid);
361 if (new_user != old_user) {
362 atomic_inc(&new_user->processes);
363 atomic_dec(&old_user->processes);
370 void vx_mask_bcaps(struct task_struct *p)
372 struct vx_info *vxi = p->vx_info;
374 p->cap_effective &= vxi->vx_bcaps;
375 p->cap_inheritable &= vxi->vx_bcaps;
376 p->cap_permitted &= vxi->vx_bcaps;
380 #include <linux/file.h>
382 static inline int vx_nofiles_task(struct task_struct *tsk)
384 struct files_struct *files = tsk->files;
385 const unsigned long *obptr, *cbptr;
388 spin_lock(&files->file_lock);
389 obptr = files->open_fds->fds_bits;
390 cbptr = files->close_on_exec->fds_bits;
391 count = files->max_fds / (sizeof(unsigned long) * 8);
392 for (total = 0; count > 0; count--) {
394 total += hweight_long(*obptr);
397 total += hweight_long(*cbptr);
400 spin_unlock(&files->file_lock);
404 static inline int vx_openfd_task(struct task_struct *tsk)
406 struct files_struct *files = tsk->files;
407 const unsigned long *bptr;
410 spin_lock(&files->file_lock);
411 bptr = files->open_fds->fds_bits;
412 count = files->max_fds / (sizeof(unsigned long) * 8);
413 for (total = 0; count > 0; count--) {
415 total += hweight_long(*bptr);
418 spin_unlock(&files->file_lock);
423 * migrate task to new context
424 * gets vxi, puts old_vxi on change
427 int vx_migrate_task(struct task_struct *p, struct vx_info *vxi)
429 struct vx_info *old_vxi;
435 old_vxi = task_get_vx_info(p);
439 vxdprintk("vx_migrate_task(%p,%p[#%d.%d)\n", p, vxi,
440 vxi->vx_id, atomic_read(&vxi->vx_usecnt));
442 if (!(ret = vx_migrate_user(p, vxi))) {
445 atomic_dec(&old_vxi->cacct.nr_threads);
446 atomic_dec(&old_vxi->limit.res[RLIMIT_NPROC]);
448 atomic_inc(&vxi->cacct.nr_threads);
449 atomic_inc(&vxi->limit.res[RLIMIT_NPROC]);
450 atomic_add(vx_nofiles_task(p), &vxi->limit.res[RLIMIT_NOFILE]);
451 atomic_add(vx_openfd_task(p), &vxi->limit.res[RLIMIT_OPENFD]);
452 /* should be handled in set_vx_info !! */
454 clr_vx_info(&p->vx_info);
455 set_vx_info(&p->vx_info, vxi);
460 put_vx_info(old_vxi);
463 put_vx_info(old_vxi);
467 int vx_set_init(struct vx_info *vxi, struct task_struct *p)
474 vxi->vx_initpid = p->tgid;
479 /* vserver syscall commands below here */
481 /* taks xid and vx_info functions */
483 #include <asm/uaccess.h>
486 int vc_task_xid(uint32_t id, void __user *data)
491 struct task_struct *tsk;
493 if (!vx_check(0, VX_ADMIN|VX_WATCH))
496 read_lock(&tasklist_lock);
497 tsk = find_task_by_pid(id);
498 xid = (tsk) ? tsk->xid : -ESRCH;
499 read_unlock(&tasklist_lock);
507 int vc_vx_info(uint32_t id, void __user *data)
510 struct vcmd_vx_info_v0 vc_data;
512 if (!vx_check(0, VX_ADMIN))
514 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RESOURCE))
517 vxi = locate_vx_info(id);
521 vc_data.xid = vxi->vx_id;
522 vc_data.initpid = vxi->vx_initpid;
525 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
531 /* context functions */
533 int vc_ctx_create(uint32_t xid, void __user *data)
535 struct vx_info *new_vxi;
538 if (!capable(CAP_SYS_ADMIN))
541 if ((xid >= MIN_D_CONTEXT) && (xid != VX_DYNAMIC_ID))
547 new_vxi = __loc_vx_info(xid, &ret);
550 if (!(new_vxi->vx_flags & VXF_STATE_SETUP)) {
555 ret = new_vxi->vx_id;
556 vx_migrate_task(current, new_vxi);
557 /* if this fails, we might end up with a hashed vx_info */
559 put_vx_info(new_vxi);
564 int vc_ctx_migrate(uint32_t id, void __user *data)
568 if (!capable(CAP_SYS_ADMIN))
571 /* dirty hack until Spectator becomes a cap */
577 vxi = locate_vx_info(id);
580 vx_migrate_task(current, vxi);
586 int vc_get_cflags(uint32_t id, void __user *data)
589 struct vcmd_ctx_flags_v0 vc_data;
591 if (!capable(CAP_SYS_ADMIN))
594 vxi = locate_vx_info(id);
598 vc_data.flagword = vxi->vx_flags;
600 /* special STATE flag handling */
601 vc_data.mask = vx_mask_flags(~0UL, vxi->vx_flags, VXF_ONE_TIME);
605 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
610 int vc_set_cflags(uint32_t id, void __user *data)
613 struct vcmd_ctx_flags_v0 vc_data;
614 uint64_t mask, trigger;
616 if (!capable(CAP_SYS_ADMIN))
618 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
621 vxi = locate_vx_info(id);
625 /* special STATE flag handling */
626 mask = vx_mask_mask(vc_data.mask, vxi->vx_flags, VXF_ONE_TIME);
627 trigger = (mask & vxi->vx_flags) ^ (mask & vc_data.flagword);
629 if (trigger & VXF_STATE_SETUP)
630 vx_mask_bcaps(current);
631 if (trigger & VXF_STATE_INIT)
632 if (vxi == current->vx_info)
633 vx_set_init(vxi, current);
635 vxi->vx_flags = vx_mask_flags(vxi->vx_flags,
636 vc_data.flagword, mask);
641 int vc_get_ccaps(uint32_t id, void __user *data)
644 struct vcmd_ctx_caps_v0 vc_data;
646 if (!capable(CAP_SYS_ADMIN))
649 vxi = locate_vx_info(id);
653 vc_data.bcaps = vxi->vx_bcaps;
654 vc_data.ccaps = vxi->vx_ccaps;
655 vc_data.cmask = ~0UL;
658 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
663 int vc_set_ccaps(uint32_t id, void __user *data)
666 struct vcmd_ctx_caps_v0 vc_data;
668 if (!capable(CAP_SYS_ADMIN))
670 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
673 vxi = locate_vx_info(id);
677 vxi->vx_bcaps &= vc_data.bcaps;
678 vxi->vx_ccaps = vx_mask_flags(vxi->vx_ccaps,
679 vc_data.ccaps, vc_data.cmask);
684 #include <linux/module.h>
686 EXPORT_SYMBOL_GPL(rcu_free_vx_info);
687 EXPORT_SYMBOL_GPL(vx_info_hash_lock);