2 * linux/kernel/vserver/context.c
4 * Virtual Server: Context Support
6 * Copyright (C) 2003-2005 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
16 * V0.09 revert to non RCU for now
17 * V0.10 and back to working RCU hash
21 #include <linux/config.h>
22 #include <linux/slab.h>
23 #include <linux/types.h>
24 #include <linux/namespace.h>
26 #include <linux/sched.h>
27 #include <linux/vserver/network.h>
28 #include <linux/vserver/legacy.h>
29 #include <linux/vserver/limit.h>
30 #include <linux/vserver/debug.h>
31 #include <linux/vs_context.h>
32 #include <linux/vserver/context_cmd.h>
34 #include <asm/errno.h>
36 #include "cvirt_init.h"
37 #include "limit_init.h"
38 #include "sched_init.h"
43 * allocate an initialized vx_info struct
44 * doesn't make it visible (hash) */
46 static struct vx_info *__alloc_vx_info(xid_t xid)
48 struct vx_info *new = NULL;
50 vxdprintk(VXD_CBIT(xid, 0), "alloc_vx_info(%d)*", xid);
52 /* would this benefit from a slab cache? */
53 new = kmalloc(sizeof(struct vx_info), GFP_KERNEL);
57 memset (new, 0, sizeof(struct vx_info));
59 INIT_RCU_HEAD(&new->vx_rcu);
60 INIT_HLIST_NODE(&new->vx_hlist);
61 atomic_set(&new->vx_refcnt, 0);
62 atomic_set(&new->vx_usecnt, 0);
63 new->vx_parent = NULL;
65 new->vx_lock = SPIN_LOCK_UNLOCKED;
66 init_waitqueue_head(&new->vx_exit);
68 /* rest of init goes here */
69 vx_info_init_limit(&new->limit);
70 vx_info_init_sched(&new->sched);
71 vx_info_init_cvirt(&new->cvirt);
72 vx_info_init_cacct(&new->cacct);
75 new->vx_flags = VXF_STATE_SETUP|VXF_STATE_INIT;
76 new->vx_bcaps = CAP_INIT_EFF_SET;
79 vxdprintk(VXD_CBIT(xid, 0),
80 "alloc_vx_info(%d) = %p", xid, new);
81 vxh_alloc_vx_info(new);
85 /* __dealloc_vx_info()
87 * final disposal of vx_info */
89 static void __dealloc_vx_info(struct vx_info *vxi)
91 vxdprintk(VXD_CBIT(xid, 0),
92 "dealloc_vx_info(%p)", vxi);
93 vxh_dealloc_vx_info(vxi);
95 vxi->vx_hlist.next = LIST_POISON1;
98 vx_info_exit_limit(&vxi->limit);
99 vx_info_exit_sched(&vxi->sched);
100 vx_info_exit_cvirt(&vxi->cvirt);
101 vx_info_exit_cacct(&vxi->cacct);
104 BUG_ON(atomic_read(&vxi->vx_usecnt));
105 BUG_ON(atomic_read(&vxi->vx_refcnt));
107 BUG_ON(vx_info_state(vxi, VXS_HASHED));
108 // BUG_ON(!vx_state(vxi, VXS_DEFUNCT));
110 vxi->vx_state |= VXS_RELEASED;
114 static inline int __free_vx_info(struct vx_info *vxi)
120 usecnt = atomic_read(&vxi->vx_usecnt);
123 refcnt = atomic_read(&vxi->vx_refcnt);
127 __dealloc_vx_info(vxi);
131 static void __rcu_put_vx_info(struct rcu_head *head)
133 struct vx_info *vxi = container_of(head, struct vx_info, vx_rcu);
135 vxdprintk(VXD_CBIT(xid, 3),
136 "__rcu_put_vx_info(%p[#%d]): %d,%d",
138 atomic_read(&vxi->vx_usecnt),
139 atomic_read(&vxi->vx_refcnt));
143 void __shutdown_vx_info(struct vx_info *vxi)
145 struct namespace *namespace;
146 struct fs_struct *fs;
150 namespace = xchg(&vxi->vx_namespace, NULL);
152 put_namespace(namespace);
154 fs = xchg(&vxi->vx_fs, NULL);
161 void free_vx_info(struct vx_info *vxi)
163 /* context shutdown is mandatory */
164 // BUG_ON(vxi->vx_state != VXS_SHUTDOWN);
166 BUG_ON(vxi->vx_state & VXS_HASHED);
168 BUG_ON(vxi->vx_namespace);
171 BUG_ON(__free_vx_info(vxi));
175 /* hash table for vx_info hash */
177 #define VX_HASH_SIZE 13
179 struct hlist_head vx_info_hash[VX_HASH_SIZE];
181 static spinlock_t vx_info_hash_lock = SPIN_LOCK_UNLOCKED;
184 static inline unsigned int __hashval(xid_t xid)
186 return (xid % VX_HASH_SIZE);
193 * add the vxi to the global hash table
194 * requires the hash_lock to be held */
196 static inline void __hash_vx_info(struct vx_info *vxi)
198 struct hlist_head *head;
200 vxdprintk(VXD_CBIT(xid, 4),
201 "__hash_vx_info: %p[#%d]", vxi, vxi->vx_id);
202 vxh_hash_vx_info(vxi);
205 vxi->vx_state |= VXS_HASHED;
206 head = &vx_info_hash[__hashval(vxi->vx_id)];
207 hlist_add_head_rcu(&vxi->vx_hlist, head);
210 /* __unhash_vx_info()
212 * remove the vxi from the global hash table
213 * requires the hash_lock to be held */
215 static inline void __unhash_vx_info(struct vx_info *vxi)
217 vxdprintk(VXD_CBIT(xid, 4),
218 "__unhash_vx_info: %p[#%d]", vxi, vxi->vx_id);
219 vxh_unhash_vx_info(vxi);
221 vxi->vx_state &= ~VXS_HASHED;
222 hlist_del_rcu(&vxi->vx_hlist);
224 call_rcu(&vxi->vx_rcu, __rcu_put_vx_info);
228 /* __lookup_vx_info()
230 * requires the rcu_read_lock()
231 * doesn't increment the vx_refcnt */
233 static inline struct vx_info *__lookup_vx_info(xid_t xid)
235 struct hlist_head *head = &vx_info_hash[__hashval(xid)];
236 struct hlist_node *pos;
239 hlist_for_each_rcu(pos, head) {
240 vxi = hlist_entry(pos, struct vx_info, vx_hlist);
242 if ((vxi->vx_id == xid) &&
243 vx_info_state(vxi, VXS_HASHED))
248 vxdprintk(VXD_CBIT(xid, 0),
249 "__lookup_vx_info(#%u): %p[#%u]",
250 xid, vxi, vxi?vxi->vx_id:0);
251 vxh_lookup_vx_info(xid, vxi);
258 * find unused dynamic xid
259 * requires the rcu_read_lock()
260 * requires the hash_lock to be held */
262 static inline xid_t __vx_dynamic_id(void)
264 static xid_t seq = MAX_S_CONTEXT;
268 if (++seq > MAX_S_CONTEXT)
270 if (!__lookup_vx_info(seq)) {
271 vxdprintk(VXD_CBIT(xid, 4),
272 "__vx_dynamic_id: [#%d]", seq);
275 } while (barrier != seq);
281 * locate or create the requested context
282 * get() it and if new hash it */
284 static struct vx_info * __loc_vx_info(int id, int *err)
286 struct vx_info *new, *vxi = NULL;
288 vxdprintk(VXD_CBIT(xid, 1), "loc_vx_info(%d)*", id);
290 if (!(new = __alloc_vx_info(id))) {
295 /* FIXME is this required at all ? */
297 /* required to make dynamic xids unique */
298 spin_lock(&vx_info_hash_lock);
300 /* dynamic context requested */
301 if (id == VX_DYNAMIC_ID) {
302 id = __vx_dynamic_id();
304 printk(KERN_ERR "no dynamic context available.\n");
309 /* existing context requested */
310 else if ((vxi = __lookup_vx_info(id))) {
311 /* context in setup is not available */
312 if (vxi->vx_flags & VXF_STATE_SETUP) {
313 vxdprintk(VXD_CBIT(xid, 0),
314 "loc_vx_info(%d) = %p (not available)", id, vxi);
318 vxdprintk(VXD_CBIT(xid, 0),
319 "loc_vx_info(%d) = %p (found)", id, vxi);
326 /* new context requested */
327 vxdprintk(VXD_CBIT(xid, 0),
328 "loc_vx_info(%d) = %p (new)", id, new);
329 __hash_vx_info(get_vx_info(new));
330 vxi = new, new = NULL;
334 spin_unlock(&vx_info_hash_lock);
336 vxh_loc_vx_info(id, vxi);
338 __dealloc_vx_info(new);
347 void unhash_vx_info(struct vx_info *vxi)
349 __shutdown_vx_info(vxi);
350 spin_lock(&vx_info_hash_lock);
351 __unhash_vx_info(vxi);
352 spin_unlock(&vx_info_hash_lock);
357 * search for a vx_info and get() it
358 * negative id means current */
360 struct vx_info *locate_vx_info(int id)
365 vxi = get_vx_info(current->vx_info);
368 vxi = get_vx_info(__lookup_vx_info(id));
374 /* vx_info_is_hashed()
376 * verify that xid is still hashed */
378 int vx_info_is_hashed(xid_t xid)
383 hashed = (__lookup_vx_info(xid) != NULL);
388 #ifdef CONFIG_VSERVER_LEGACY
391 struct vx_info *alloc_vx_info(xid_t xid)
393 return __alloc_vx_info(xid);
397 struct vx_info *locate_or_create_vx_info(int id)
401 return __loc_vx_info(id, &err);
406 #ifdef CONFIG_PROC_FS
408 int get_xid_list(int index, unsigned int *xids, int size)
410 int hindex, nr_xids = 0;
413 for (hindex = 0; hindex < VX_HASH_SIZE; hindex++) {
414 struct hlist_head *head = &vx_info_hash[hindex];
415 struct hlist_node *pos;
417 hlist_for_each_rcu(pos, head) {
423 vxi = hlist_entry(pos, struct vx_info, vx_hlist);
424 xids[nr_xids] = vxi->vx_id;
425 if (++nr_xids >= size)
435 int vx_migrate_user(struct task_struct *p, struct vx_info *vxi)
437 struct user_struct *new_user, *old_user;
441 new_user = alloc_uid(vxi->vx_id, p->uid);
446 if (new_user != old_user) {
447 atomic_inc(&new_user->processes);
448 atomic_dec(&old_user->processes);
455 void vx_mask_bcaps(struct task_struct *p)
457 struct vx_info *vxi = p->vx_info;
459 p->cap_effective &= vxi->vx_bcaps;
460 p->cap_inheritable &= vxi->vx_bcaps;
461 p->cap_permitted &= vxi->vx_bcaps;
465 #include <linux/file.h>
467 static inline int vx_nofiles_task(struct task_struct *tsk)
469 struct files_struct *files = tsk->files;
470 unsigned long *obptr;
473 spin_lock(&files->file_lock);
474 obptr = files->open_fds->fds_bits;
475 count = files->max_fds / (sizeof(unsigned long) * 8);
476 for (total = 0; count > 0; count--) {
478 total += hweight_long(*obptr);
481 spin_unlock(&files->file_lock);
487 static inline int vx_openfd_task(struct task_struct *tsk)
489 struct files_struct *files = tsk->files;
490 const unsigned long *bptr;
493 spin_lock(&files->file_lock);
494 bptr = files->open_fds->fds_bits;
495 count = files->max_fds / (sizeof(unsigned long) * 8);
496 for (total = 0; count > 0; count--) {
498 total += hweight_long(*bptr);
501 spin_unlock(&files->file_lock);
508 * migrate task to new context
509 * gets vxi, puts old_vxi on change
512 int vx_migrate_task(struct task_struct *p, struct vx_info *vxi)
514 struct vx_info *old_vxi;
520 old_vxi = task_get_vx_info(p);
524 vxdprintk(VXD_CBIT(xid, 5),
525 "vx_migrate_task(%p,%p[#%d.%d])", p, vxi,
526 vxi->vx_id, atomic_read(&vxi->vx_usecnt));
528 if (!(ret = vx_migrate_user(p, vxi))) {
532 // openfd = vx_openfd_task(p);
533 nofiles = vx_nofiles_task(p);
536 atomic_dec(&old_vxi->cvirt.nr_threads);
537 atomic_dec(&old_vxi->cvirt.nr_running);
538 atomic_dec(&old_vxi->limit.rcur[RLIMIT_NPROC]);
539 /* FIXME: what about the struct files here? */
540 // atomic_sub(nofiles, &old_vxi->limit.rcur[RLIMIT_NOFILE]);
541 // atomic_sub(openfd, &old_vxi->limit.rcur[RLIMIT_OPENFD]);
543 atomic_inc(&vxi->cvirt.nr_threads);
544 atomic_inc(&vxi->cvirt.nr_running);
545 atomic_inc(&vxi->limit.rcur[RLIMIT_NPROC]);
546 /* FIXME: what about the struct files here? */
547 // atomic_add(nofiles, &vxi->limit.rcur[RLIMIT_NOFILE]);
548 // atomic_add(openfd, &vxi->limit.rcur[RLIMIT_OPENFD]);
550 vxdprintk(VXD_CBIT(xid, 5),
551 "moved task %p into vxi:%p[#%d]",
554 /* should be handled in set_vx_info !! */
556 clr_vx_info(&p->vx_info);
557 set_vx_info(&p->vx_info, vxi);
562 /* obsoleted by clr/set */
563 // put_vx_info(old_vxi);
567 put_vx_info(old_vxi);
571 int vx_set_init(struct vx_info *vxi, struct task_struct *p)
578 vxdprintk(VXD_CBIT(xid, 6),
579 "vx_set_init(%p[#%d],%p[#%d,%d,%d])",
580 vxi, vxi->vx_id, p, p->xid, p->pid, p->tgid);
582 vxi->vx_initpid = p->tgid;
587 /* vserver syscall commands below here */
589 /* taks xid and vx_info functions */
591 #include <asm/uaccess.h>
594 int vc_task_xid(uint32_t id, void __user *data)
599 struct task_struct *tsk;
601 if (!vx_check(0, VX_ADMIN|VX_WATCH))
604 read_lock(&tasklist_lock);
605 tsk = find_task_by_real_pid(id);
606 xid = (tsk) ? tsk->xid : -ESRCH;
607 read_unlock(&tasklist_lock);
610 xid = vx_current_xid();
615 int vc_vx_info(uint32_t id, void __user *data)
618 struct vcmd_vx_info_v0 vc_data;
620 if (!vx_check(0, VX_ADMIN))
622 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RESOURCE))
625 vxi = locate_vx_info(id);
629 vc_data.xid = vxi->vx_id;
630 vc_data.initpid = vxi->vx_initpid;
633 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
639 /* context functions */
641 int vc_ctx_create(uint32_t xid, void __user *data)
643 struct vx_info *new_vxi;
646 if (!capable(CAP_SYS_ADMIN))
649 if ((xid >= MIN_D_CONTEXT) && (xid != VX_DYNAMIC_ID))
655 new_vxi = __loc_vx_info(xid, &ret);
658 if (!(new_vxi->vx_flags & VXF_STATE_SETUP)) {
663 ret = new_vxi->vx_id;
664 vx_migrate_task(current, new_vxi);
665 /* if this fails, we might end up with a hashed vx_info */
667 put_vx_info(new_vxi);
672 int vc_ctx_migrate(uint32_t id, void __user *data)
676 if (!capable(CAP_SYS_ADMIN))
679 /* dirty hack until Spectator becomes a cap */
685 vxi = locate_vx_info(id);
688 vx_migrate_task(current, vxi);
694 int vc_get_cflags(uint32_t id, void __user *data)
697 struct vcmd_ctx_flags_v0 vc_data;
699 if (!capable(CAP_SYS_ADMIN))
702 vxi = locate_vx_info(id);
706 vc_data.flagword = vxi->vx_flags;
708 /* special STATE flag handling */
709 vc_data.mask = vx_mask_flags(~0UL, vxi->vx_flags, VXF_ONE_TIME);
713 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
718 int vc_set_cflags(uint32_t id, void __user *data)
721 struct vcmd_ctx_flags_v0 vc_data;
722 uint64_t mask, trigger;
724 if (!capable(CAP_SYS_ADMIN))
726 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
729 vxi = locate_vx_info(id);
733 /* special STATE flag handling */
734 mask = vx_mask_mask(vc_data.mask, vxi->vx_flags, VXF_ONE_TIME);
735 trigger = (mask & vxi->vx_flags) ^ (mask & vc_data.flagword);
737 if (trigger & VXF_STATE_SETUP)
738 vx_mask_bcaps(current);
739 if (trigger & VXF_STATE_INIT)
740 if (vxi == current->vx_info)
741 vx_set_init(vxi, current);
743 vxi->vx_flags = vx_mask_flags(vxi->vx_flags,
744 vc_data.flagword, mask);
749 int vc_get_ccaps(uint32_t id, void __user *data)
752 struct vcmd_ctx_caps_v0 vc_data;
754 if (!capable(CAP_SYS_ADMIN))
757 vxi = locate_vx_info(id);
761 vc_data.bcaps = vxi->vx_bcaps;
762 vc_data.ccaps = vxi->vx_ccaps;
763 vc_data.cmask = ~0UL;
766 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
771 int vc_set_ccaps(uint32_t id, void __user *data)
774 struct vcmd_ctx_caps_v0 vc_data;
776 if (!capable(CAP_SYS_ADMIN))
778 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
781 vxi = locate_vx_info(id);
785 vxi->vx_bcaps &= vc_data.bcaps;
786 vxi->vx_ccaps = vx_mask_flags(vxi->vx_ccaps,
787 vc_data.ccaps, vc_data.cmask);
792 #include <linux/module.h>
794 EXPORT_SYMBOL_GPL(free_vx_info);
795 EXPORT_SYMBOL_GPL(unhash_vx_info);