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.h>
22 #include <linux/vserver/legacy.h>
23 #include <linux/vs_base.h>
24 #include <linux/vs_context.h>
25 #include <linux/kernel_stat.h>
26 #include <linux/namespace.h>
27 #include <linux/rcupdate.h>
29 #define CKRM_VSERVER_INTEGRATION
30 #ifdef CKRM_VSERVER_INTEGRATION
31 #include <linux/ckrm.h>
32 #endif //CKRM_VSERVER_INTEGRATION
34 #include <asm/errno.h>
39 * allocate an initialized vx_info struct
40 * doesn't make it visible (hash) */
42 static struct vx_info *__alloc_vx_info(xid_t xid)
44 struct vx_info *new = NULL;
46 vxdprintk(VXD_CBIT(xid, 0), "alloc_vx_info(%d)*", xid);
48 /* would this benefit from a slab cache? */
49 new = kmalloc(sizeof(struct vx_info), GFP_KERNEL);
53 memset (new, 0, sizeof(struct vx_info));
55 INIT_RCU_HEAD(&new->vx_rcu);
56 INIT_HLIST_NODE(&new->vx_hlist);
57 atomic_set(&new->vx_refcnt, 0);
58 atomic_set(&new->vx_usecnt, 0);
59 new->vx_parent = NULL;
61 new->vx_lock = SPIN_LOCK_UNLOCKED;
62 init_waitqueue_head(&new->vx_exit);
64 /* rest of init goes here */
65 vx_info_init_limit(&new->limit);
66 vx_info_init_sched(&new->sched);
67 vx_info_init_cvirt(&new->cvirt);
68 vx_info_init_cacct(&new->cacct);
71 new->vx_flags = VXF_STATE_SETUP|VXF_STATE_INIT;
72 new->vx_bcaps = CAP_INIT_EFF_SET;
75 vxdprintk(VXD_CBIT(xid, 0),
76 "alloc_vx_info(%d) = %p", xid, new);
80 /* __dealloc_vx_info()
82 * final disposal of vx_info */
84 static void __dealloc_vx_info(struct vx_info *vxi)
86 vxdprintk(VXD_CBIT(xid, 0),
87 "dealloc_vx_info(%p)", vxi);
89 vxi->vx_hlist.next = LIST_POISON1;
92 vx_info_exit_limit(&vxi->limit);
93 vx_info_exit_sched(&vxi->sched);
94 vx_info_exit_cvirt(&vxi->cvirt);
95 vx_info_exit_cacct(&vxi->cacct);
98 BUG_ON(atomic_read(&vxi->vx_usecnt));
99 BUG_ON(atomic_read(&vxi->vx_refcnt));
101 BUG_ON(vx_info_state(vxi, VXS_HASHED));
102 // BUG_ON(!vx_state(vxi, VXS_DEFUNCT));
104 vxi->vx_state |= VXS_RELEASED;
108 static inline int __free_vx_info(struct vx_info *vxi)
114 usecnt = atomic_read(&vxi->vx_usecnt);
117 refcnt = atomic_read(&vxi->vx_refcnt);
121 __dealloc_vx_info(vxi);
127 static void __rcu_free_vx_info(struct rcu_head *head)
129 struct vx_info *vxi = container_of(head, struct vx_info, vx_rcu);
132 vxdprintk(VXD_CBIT(xid, 3),
133 "rcu_free_vx_info(%p): uc=%d", vxi,
134 atomic_read(&vxi->vx_usecnt));
141 void free_vx_info(struct vx_info *vxi)
143 struct namespace *namespace;
144 struct fs_struct *fs;
146 /* context shutdown is mandatory */
147 // BUG_ON(vxi->vx_state != VXS_SHUTDOWN);
149 namespace = xchg(&vxi->vx_namespace, NULL);
150 fs = xchg(&vxi->vx_fs, NULL);
153 put_namespace(namespace);
157 BUG_ON(__free_vx_info(vxi));
158 // call_rcu(&i->vx_rcu, __rcu_free_vx_info);
162 /* hash table for vx_info hash */
164 #define VX_HASH_SIZE 13
166 struct hlist_head vx_info_hash[VX_HASH_SIZE];
168 static spinlock_t vx_info_hash_lock = SPIN_LOCK_UNLOCKED;
171 static inline unsigned int __hashval(xid_t xid)
173 return (xid % VX_HASH_SIZE);
180 * add the vxi to the global hash table
181 * requires the hash_lock to be held */
183 static inline void __hash_vx_info(struct vx_info *vxi)
185 struct hlist_head *head;
187 vxdprintk(VXD_CBIT(xid, 4),
188 "__hash_vx_info: %p[#%d]", vxi, vxi->vx_id);
190 vxi->vx_state |= VXS_HASHED;
191 head = &vx_info_hash[__hashval(vxi->vx_id)];
192 hlist_add_head_rcu(&vxi->vx_hlist, head);
195 /* __unhash_vx_info()
197 * remove the vxi from the global hash table
198 * requires the hash_lock to be held */
200 static inline void __unhash_vx_info(struct vx_info *vxi)
202 vxdprintk(VXD_CBIT(xid, 4),
203 "__unhash_vx_info: %p[#%d]", vxi, vxi->vx_id);
204 vxi->vx_state &= ~VXS_HASHED;
205 hlist_del_rcu(&vxi->vx_hlist);
210 /* __lookup_vx_info()
212 * requires the rcu_read_lock()
213 * doesn't increment the vx_refcnt */
215 static inline struct vx_info *__lookup_vx_info(xid_t xid)
217 struct hlist_head *head = &vx_info_hash[__hashval(xid)];
218 struct hlist_node *pos;
220 hlist_for_each_rcu(pos, head) {
221 struct vx_info *vxi =
222 hlist_entry(pos, struct vx_info, vx_hlist);
224 if ((vxi->vx_id == xid) &&
225 vx_info_state(vxi, VXS_HASHED))
234 * find unused dynamic xid
235 * requires the hash_lock to be held */
237 static inline xid_t __vx_dynamic_id(void)
239 static xid_t seq = MAX_S_CONTEXT;
243 if (++seq > MAX_S_CONTEXT)
245 if (!__lookup_vx_info(seq)) {
246 vxdprintk(VXD_CBIT(xid, 4),
247 "__vx_dynamic_id: [#%d]", seq);
250 } while (barrier != seq);
256 * locate or create the requested context
257 * get() it and if new hash it */
259 static struct vx_info * __loc_vx_info(int id, int *err)
261 struct vx_info *new, *vxi = NULL;
263 vxdprintk(VXD_CBIT(xid, 1), "loc_vx_info(%d)*", id);
265 if (!(new = __alloc_vx_info(id))) {
270 spin_lock(&vx_info_hash_lock);
272 /* dynamic context requested */
273 if (id == VX_DYNAMIC_ID) {
274 id = __vx_dynamic_id();
276 printk(KERN_ERR "no dynamic context available.\n");
281 /* existing context requested */
282 else if ((vxi = __lookup_vx_info(id))) {
283 /* context in setup is not available */
284 if (vxi->vx_flags & VXF_STATE_SETUP) {
285 vxdprintk(VXD_CBIT(xid, 0),
286 "loc_vx_info(%d) = %p (not available)", id, vxi);
290 vxdprintk(VXD_CBIT(xid, 0),
291 "loc_vx_info(%d) = %p (found)", id, vxi);
298 /* new context requested */
299 vxdprintk(VXD_CBIT(xid, 0),
300 "loc_vx_info(%d) = %p (new)", id, new);
301 __hash_vx_info(get_vx_info(new));
302 vxi = new, new = NULL;
306 spin_unlock(&vx_info_hash_lock);
308 __dealloc_vx_info(new);
317 void unhash_vx_info(struct vx_info *vxi)
319 spin_lock(&vx_info_hash_lock);
320 __unhash_vx_info(vxi);
321 spin_unlock(&vx_info_hash_lock);
326 * search for a vx_info and get() it
327 * negative id means current */
329 struct vx_info *locate_vx_info(int id)
334 vxi = get_vx_info(current->vx_info);
337 vxi = get_vx_info(__lookup_vx_info(id));
343 /* vx_info_is_hashed()
345 * verify that xid is still hashed */
347 int vx_info_is_hashed(xid_t xid)
352 hashed = (__lookup_vx_info(xid) != NULL);
357 #ifdef CONFIG_VSERVER_LEGACY
360 struct vx_info *alloc_vx_info(xid_t xid)
362 return __alloc_vx_info(xid);
366 struct vx_info *locate_or_create_vx_info(int id)
370 return __loc_vx_info(id, &err);
375 #ifdef CONFIG_PROC_FS
377 int get_xid_list(int index, unsigned int *xids, int size)
379 int hindex, nr_xids = 0;
382 for (hindex = 0; hindex < VX_HASH_SIZE; hindex++) {
383 struct hlist_head *head = &vx_info_hash[hindex];
384 struct hlist_node *pos;
386 hlist_for_each_rcu(pos, head) {
392 vxi = hlist_entry(pos, struct vx_info, vx_hlist);
393 xids[nr_xids] = vxi->vx_id;
394 if (++nr_xids >= size)
404 int vx_migrate_user(struct task_struct *p, struct vx_info *vxi)
406 struct user_struct *new_user, *old_user;
410 new_user = alloc_uid(vxi->vx_id, p->uid);
415 if (new_user != old_user) {
416 atomic_inc(&new_user->processes);
417 atomic_dec(&old_user->processes);
424 void vx_mask_bcaps(struct task_struct *p)
426 struct vx_info *vxi = p->vx_info;
428 p->cap_effective &= vxi->vx_bcaps;
429 p->cap_inheritable &= vxi->vx_bcaps;
430 p->cap_permitted &= vxi->vx_bcaps;
434 #include <linux/file.h>
436 static inline int vx_nofiles_task(struct task_struct *tsk)
438 struct files_struct *files = tsk->files;
439 unsigned long *obptr;
442 spin_lock(&files->file_lock);
443 obptr = files->open_fds->fds_bits;
444 count = files->max_fds / (sizeof(unsigned long) * 8);
445 for (total = 0; count > 0; count--) {
447 total += hweight_long(*obptr);
450 spin_unlock(&files->file_lock);
456 static inline int vx_openfd_task(struct task_struct *tsk)
458 struct files_struct *files = tsk->files;
459 const unsigned long *bptr;
462 spin_lock(&files->file_lock);
463 bptr = files->open_fds->fds_bits;
464 count = files->max_fds / (sizeof(unsigned long) * 8);
465 for (total = 0; count > 0; count--) {
467 total += hweight_long(*bptr);
470 spin_unlock(&files->file_lock);
477 * migrate task to new context
478 * gets vxi, puts old_vxi on change
481 int vx_migrate_task(struct task_struct *p, struct vx_info *vxi)
483 struct vx_info *old_vxi;
489 old_vxi = task_get_vx_info(p);
493 vxdprintk(VXD_CBIT(xid, 5),
494 "vx_migrate_task(%p,%p[#%d.%d])", p, vxi,
495 vxi->vx_id, atomic_read(&vxi->vx_usecnt));
497 if (!(ret = vx_migrate_user(p, vxi))) {
501 // openfd = vx_openfd_task(p);
502 nofiles = vx_nofiles_task(p);
505 atomic_dec(&old_vxi->cvirt.nr_threads);
506 atomic_dec(&old_vxi->cvirt.nr_running);
507 atomic_dec(&old_vxi->limit.rcur[RLIMIT_NPROC]);
508 /* FIXME: what about the struct files here? */
509 // atomic_sub(nofiles, &old_vxi->limit.rcur[RLIMIT_NOFILE]);
510 // atomic_sub(openfd, &old_vxi->limit.rcur[RLIMIT_OPENFD]);
512 atomic_inc(&vxi->cvirt.nr_threads);
513 atomic_inc(&vxi->cvirt.nr_running);
514 atomic_inc(&vxi->limit.rcur[RLIMIT_NPROC]);
515 /* FIXME: what about the struct files here? */
516 // atomic_add(nofiles, &vxi->limit.rcur[RLIMIT_NOFILE]);
517 // atomic_add(openfd, &vxi->limit.rcur[RLIMIT_OPENFD]);
519 vxdprintk(VXD_CBIT(xid, 5),
520 "moved task %p into vxi:%p[#%d]",
523 /* should be handled in set_vx_info !! */
525 clr_vx_info(&p->vx_info);
526 set_vx_info(&p->vx_info, vxi);
531 /* obsoleted by clr/set */
532 // put_vx_info(old_vxi);
537 #ifdef CKRM_VSERVER_INTEGRATION
541 #endif //CKRM_VSERVER_INTEGRATION
544 put_vx_info(old_vxi);
548 int vx_set_init(struct vx_info *vxi, struct task_struct *p)
555 vxdprintk(VXD_CBIT(xid, 6),
556 "vx_set_init(%p[#%d],%p[#%d,%d,%d])",
557 vxi, vxi->vx_id, p, p->xid, p->pid, p->tgid);
559 vxi->vx_initpid = p->tgid;
564 /* vserver syscall commands below here */
566 /* taks xid and vx_info functions */
568 #include <asm/uaccess.h>
571 int vc_task_xid(uint32_t id, void __user *data)
576 struct task_struct *tsk;
578 if (!vx_check(0, VX_ADMIN|VX_WATCH))
581 read_lock(&tasklist_lock);
582 tsk = find_task_by_real_pid(id);
583 xid = (tsk) ? tsk->xid : -ESRCH;
584 read_unlock(&tasklist_lock);
592 int vc_vx_info(uint32_t id, void __user *data)
595 struct vcmd_vx_info_v0 vc_data;
597 if (!vx_check(0, VX_ADMIN))
599 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RESOURCE))
602 vxi = locate_vx_info(id);
606 vc_data.xid = vxi->vx_id;
607 vc_data.initpid = vxi->vx_initpid;
610 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
616 /* context functions */
618 int vc_ctx_create(uint32_t xid, void __user *data)
620 struct vx_info *new_vxi;
623 if (!capable(CAP_SYS_ADMIN))
626 if ((xid >= MIN_D_CONTEXT) && (xid != VX_DYNAMIC_ID))
632 new_vxi = __loc_vx_info(xid, &ret);
635 if (!(new_vxi->vx_flags & VXF_STATE_SETUP)) {
640 ret = new_vxi->vx_id;
641 vx_migrate_task(current, new_vxi);
642 /* if this fails, we might end up with a hashed vx_info */
644 put_vx_info(new_vxi);
649 int vc_ctx_migrate(uint32_t id, void __user *data)
653 if (!capable(CAP_SYS_ADMIN))
656 /* dirty hack until Spectator becomes a cap */
662 vxi = locate_vx_info(id);
665 vx_migrate_task(current, vxi);
671 int vc_get_cflags(uint32_t id, void __user *data)
674 struct vcmd_ctx_flags_v0 vc_data;
676 if (!capable(CAP_SYS_ADMIN))
679 vxi = locate_vx_info(id);
683 vc_data.flagword = vxi->vx_flags;
685 /* special STATE flag handling */
686 vc_data.mask = vx_mask_flags(~0UL, vxi->vx_flags, VXF_ONE_TIME);
690 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
695 int vc_set_cflags(uint32_t id, void __user *data)
698 struct vcmd_ctx_flags_v0 vc_data;
699 uint64_t mask, trigger;
701 if (!capable(CAP_SYS_ADMIN))
703 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
706 vxi = locate_vx_info(id);
710 /* special STATE flag handling */
711 mask = vx_mask_mask(vc_data.mask, vxi->vx_flags, VXF_ONE_TIME);
712 trigger = (mask & vxi->vx_flags) ^ (mask & vc_data.flagword);
714 if (trigger & VXF_STATE_SETUP)
715 vx_mask_bcaps(current);
716 if (trigger & VXF_STATE_INIT)
717 if (vxi == current->vx_info)
718 vx_set_init(vxi, current);
720 vxi->vx_flags = vx_mask_flags(vxi->vx_flags,
721 vc_data.flagword, mask);
726 int vc_get_ccaps(uint32_t id, void __user *data)
729 struct vcmd_ctx_caps_v0 vc_data;
731 if (!capable(CAP_SYS_ADMIN))
734 vxi = locate_vx_info(id);
738 vc_data.bcaps = vxi->vx_bcaps;
739 vc_data.ccaps = vxi->vx_ccaps;
740 vc_data.cmask = ~0UL;
743 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
748 int vc_set_ccaps(uint32_t id, void __user *data)
751 struct vcmd_ctx_caps_v0 vc_data;
753 if (!capable(CAP_SYS_ADMIN))
755 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
758 vxi = locate_vx_info(id);
762 vxi->vx_bcaps &= vc_data.bcaps;
763 vxi->vx_ccaps = vx_mask_flags(vxi->vx_ccaps,
764 vc_data.ccaps, vc_data.cmask);
769 #include <linux/module.h>
771 // EXPORT_SYMBOL_GPL(rcu_free_vx_info);
772 EXPORT_SYMBOL_GPL(free_vx_info);
773 EXPORT_SYMBOL_GPL(vx_info_hash_lock);
774 EXPORT_SYMBOL_GPL(unhash_vx_info);