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
18 * V0.11 and back to locking again
22 #include <linux/config.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
25 #include <linux/namespace.h>
27 #include <linux/sched.h>
28 #include <linux/vserver/network.h>
29 #include <linux/vserver/legacy.h>
30 #include <linux/vserver/limit.h>
31 #include <linux/vserver/debug.h>
33 #include <linux/vs_context.h>
34 #include <linux/vserver/context_cmd.h>
36 #include <linux/err.h>
37 #include <asm/errno.h>
39 #include "cvirt_init.h"
40 #include "limit_init.h"
41 #include "sched_init.h"
46 * allocate an initialized vx_info struct
47 * doesn't make it visible (hash) */
49 static struct vx_info *__alloc_vx_info(xid_t xid)
51 struct vx_info *new = NULL;
53 vxdprintk(VXD_CBIT(xid, 0), "alloc_vx_info(%d)*", xid);
55 /* would this benefit from a slab cache? */
56 new = kmalloc(sizeof(struct vx_info), GFP_KERNEL);
60 memset (new, 0, sizeof(struct vx_info));
62 // INIT_RCU_HEAD(&new->vx_rcu);
63 INIT_HLIST_NODE(&new->vx_hlist);
64 atomic_set(&new->vx_usecnt, 0);
65 atomic_set(&new->vx_tasks, 0);
66 new->vx_parent = NULL;
68 new->vx_lock = SPIN_LOCK_UNLOCKED;
69 init_waitqueue_head(&new->vx_exit);
71 /* rest of init goes here */
72 vx_info_init_limit(&new->limit);
73 vx_info_init_sched(&new->sched);
74 vx_info_init_cvirt(&new->cvirt);
75 vx_info_init_cacct(&new->cacct);
78 new->vx_flags = VXF_STATE_SETUP|VXF_STATE_INIT;
79 new->vx_bcaps = CAP_INIT_EFF_SET;
82 vxdprintk(VXD_CBIT(xid, 0),
83 "alloc_vx_info(%d) = %p", xid, new);
84 vxh_alloc_vx_info(new);
88 /* __dealloc_vx_info()
90 * final disposal of vx_info */
92 static void __dealloc_vx_info(struct vx_info *vxi)
94 vxdprintk(VXD_CBIT(xid, 0),
95 "dealloc_vx_info(%p)", vxi);
96 vxh_dealloc_vx_info(vxi);
98 vxi->vx_hlist.next = LIST_POISON1;
101 vx_info_exit_limit(&vxi->limit);
102 vx_info_exit_sched(&vxi->sched);
103 vx_info_exit_cvirt(&vxi->cvirt);
104 vx_info_exit_cacct(&vxi->cacct);
106 vxi->vx_state |= VXS_RELEASED;
110 void __shutdown_vx_info(struct vx_info *vxi)
112 struct namespace *namespace;
113 struct fs_struct *fs;
117 namespace = xchg(&vxi->vx_namespace, NULL);
119 put_namespace(namespace);
121 fs = xchg(&vxi->vx_fs, NULL);
128 void free_vx_info(struct vx_info *vxi)
130 /* context shutdown is mandatory */
131 // BUG_ON(vxi->vx_state != VXS_SHUTDOWN);
133 BUG_ON(atomic_read(&vxi->vx_usecnt));
134 BUG_ON(atomic_read(&vxi->vx_tasks));
136 BUG_ON(vx_info_state(vxi, VXS_HASHED));
137 // BUG_ON(!vx_state(vxi, VXS_DEFUNCT));
139 BUG_ON(vxi->vx_namespace);
142 __dealloc_vx_info(vxi);
146 /* hash table for vx_info hash */
148 #define VX_HASH_SIZE 13
150 struct hlist_head vx_info_hash[VX_HASH_SIZE];
152 static spinlock_t vx_info_hash_lock = SPIN_LOCK_UNLOCKED;
155 static inline unsigned int __hashval(xid_t xid)
157 return (xid % VX_HASH_SIZE);
164 * add the vxi to the global hash table
165 * requires the hash_lock to be held */
167 static inline void __hash_vx_info(struct vx_info *vxi)
169 struct hlist_head *head;
171 vxd_assert_lock(&vx_info_hash_lock);
172 vxdprintk(VXD_CBIT(xid, 4),
173 "__hash_vx_info: %p[#%d]", vxi, vxi->vx_id);
174 vxh_hash_vx_info(vxi);
176 /* context must not be hashed */
177 BUG_ON(vxi->vx_state & VXS_HASHED);
180 vxi->vx_state |= VXS_HASHED;
181 head = &vx_info_hash[__hashval(vxi->vx_id)];
182 hlist_add_head(&vxi->vx_hlist, head);
185 /* __unhash_vx_info()
187 * remove the vxi from the global hash table
188 * requires the hash_lock to be held */
190 static inline void __unhash_vx_info(struct vx_info *vxi)
192 vxd_assert_lock(&vx_info_hash_lock);
193 vxdprintk(VXD_CBIT(xid, 4),
194 "__unhash_vx_info: %p[#%d]", vxi, vxi->vx_id);
195 vxh_unhash_vx_info(vxi);
197 /* maybe warn on that? */
198 if (!(vxi->vx_state & VXS_HASHED))
201 vxi->vx_state &= ~VXS_HASHED;
202 hlist_del(&vxi->vx_hlist);
207 /* __lookup_vx_info()
209 * requires the hash_lock to be held
210 * doesn't increment the vx_refcnt */
212 static inline struct vx_info *__lookup_vx_info(xid_t xid)
214 struct hlist_head *head = &vx_info_hash[__hashval(xid)];
215 struct hlist_node *pos;
218 vxd_assert_lock(&vx_info_hash_lock);
219 hlist_for_each(pos, head) {
220 vxi = hlist_entry(pos, struct vx_info, vx_hlist);
222 if (vxi->vx_id == xid)
227 vxdprintk(VXD_CBIT(xid, 0),
228 "__lookup_vx_info(#%u): %p[#%u]",
229 xid, vxi, vxi?vxi->vx_id:0);
230 vxh_lookup_vx_info(xid, vxi);
237 * find unused dynamic xid
238 * requires the hash_lock to be held */
240 static inline xid_t __vx_dynamic_id(void)
242 static xid_t seq = MAX_S_CONTEXT;
245 vxd_assert_lock(&vx_info_hash_lock);
247 if (++seq > MAX_S_CONTEXT)
249 if (!__lookup_vx_info(seq)) {
250 vxdprintk(VXD_CBIT(xid, 4),
251 "__vx_dynamic_id: [#%d]", seq);
254 } while (barrier != seq);
258 #ifdef CONFIG_VSERVER_LEGACY
262 * locate or create the requested context
263 * get() it and if new hash it */
265 static struct vx_info * __loc_vx_info(int id, int *err)
267 struct vx_info *new, *vxi = NULL;
269 vxdprintk(VXD_CBIT(xid, 1), "loc_vx_info(%d)*", id);
271 if (!(new = __alloc_vx_info(id))) {
276 /* required to make dynamic xids unique */
277 spin_lock(&vx_info_hash_lock);
279 /* dynamic context requested */
280 if (id == VX_DYNAMIC_ID) {
281 id = __vx_dynamic_id();
283 printk(KERN_ERR "no dynamic context available.\n");
288 /* existing context requested */
289 else if ((vxi = __lookup_vx_info(id))) {
290 /* context in setup is not available */
291 if (vxi->vx_flags & VXF_STATE_SETUP) {
292 vxdprintk(VXD_CBIT(xid, 0),
293 "loc_vx_info(%d) = %p (not available)", id, vxi);
297 vxdprintk(VXD_CBIT(xid, 0),
298 "loc_vx_info(%d) = %p (found)", id, vxi);
305 /* new context requested */
306 vxdprintk(VXD_CBIT(xid, 0),
307 "loc_vx_info(%d) = %p (new)", id, new);
308 __hash_vx_info(get_vx_info(new));
309 vxi = new, new = NULL;
313 spin_unlock(&vx_info_hash_lock);
314 vxh_loc_vx_info(id, vxi);
316 __dealloc_vx_info(new);
322 /* __create_vx_info()
324 * create the requested context
325 * get() it and hash it */
327 static struct vx_info * __create_vx_info(int id)
329 struct vx_info *new, *vxi = NULL;
331 vxdprintk(VXD_CBIT(xid, 1), "create_vx_info(%d)*", id);
333 if (!(new = __alloc_vx_info(id))) {
334 return ERR_PTR(-ENOMEM);
337 /* required to make dynamic xids unique */
338 spin_lock(&vx_info_hash_lock);
340 /* dynamic context requested */
341 if (id == VX_DYNAMIC_ID) {
342 id = __vx_dynamic_id();
344 printk(KERN_ERR "no dynamic context available.\n");
345 vxi = ERR_PTR(-EAGAIN);
350 /* existing context requested */
351 else if ((vxi = __lookup_vx_info(id))) {
352 vxdprintk(VXD_CBIT(xid, 0),
353 "create_vx_info(%d) = %p (already there)", id, vxi);
354 if (vx_info_flags(vxi, VXF_STATE_SETUP, 0))
355 vxi = ERR_PTR(-EBUSY);
357 vxi = ERR_PTR(-EEXIST);
360 /* dynamic xid creation blocker */
361 else if (id >= MIN_D_CONTEXT) {
362 vxdprintk(VXD_CBIT(xid, 0),
363 "create_vx_info(%d) (dynamic rejected)", id);
364 vxi = ERR_PTR(-EINVAL);
368 /* new context requested */
369 vxdprintk(VXD_CBIT(xid, 0),
370 "create_vx_info(%d) = %p (new)", id, new);
371 __hash_vx_info(get_vx_info(new));
372 vxi = new, new = NULL;
375 spin_unlock(&vx_info_hash_lock);
376 vxh_create_vx_info(id, IS_ERR(vxi)?NULL:vxi);
378 __dealloc_vx_info(new);
386 void unhash_vx_info(struct vx_info *vxi)
388 __shutdown_vx_info(vxi);
389 spin_lock(&vx_info_hash_lock);
390 __unhash_vx_info(vxi);
391 spin_unlock(&vx_info_hash_lock);
397 * search for a vx_info and get() it
398 * negative id means current */
400 struct vx_info *locate_vx_info(int id)
402 struct vx_info *vxi = NULL;
405 vxi = get_vx_info(current->vx_info);
407 spin_lock(&vx_info_hash_lock);
408 vxi = get_vx_info(__lookup_vx_info(id));
409 spin_unlock(&vx_info_hash_lock);
416 * verify that xid is still hashed */
418 int xid_is_hashed(xid_t xid)
422 spin_lock(&vx_info_hash_lock);
423 hashed = (__lookup_vx_info(xid) != NULL);
424 spin_unlock(&vx_info_hash_lock);
428 #ifdef CONFIG_VSERVER_LEGACY
430 struct vx_info *locate_or_create_vx_info(int id)
434 return __loc_vx_info(id, &err);
439 #ifdef CONFIG_PROC_FS
441 int get_xid_list(int index, unsigned int *xids, int size)
443 int hindex, nr_xids = 0;
445 for (hindex = 0; hindex < VX_HASH_SIZE; hindex++) {
446 struct hlist_head *head = &vx_info_hash[hindex];
447 struct hlist_node *pos;
449 spin_lock(&vx_info_hash_lock);
450 hlist_for_each(pos, head) {
456 vxi = hlist_entry(pos, struct vx_info, vx_hlist);
457 xids[nr_xids] = vxi->vx_id;
458 if (++nr_xids >= size) {
459 spin_unlock(&vx_info_hash_lock);
463 /* keep the lock time short */
464 spin_unlock(&vx_info_hash_lock);
471 int vx_migrate_user(struct task_struct *p, struct vx_info *vxi)
473 struct user_struct *new_user, *old_user;
477 new_user = alloc_uid(vxi->vx_id, p->uid);
482 if (new_user != old_user) {
483 atomic_inc(&new_user->processes);
484 atomic_dec(&old_user->processes);
491 void vx_mask_bcaps(struct task_struct *p)
493 struct vx_info *vxi = p->vx_info;
495 p->cap_effective &= vxi->vx_bcaps;
496 p->cap_inheritable &= vxi->vx_bcaps;
497 p->cap_permitted &= vxi->vx_bcaps;
501 #include <linux/file.h>
503 static int vx_openfd_task(struct task_struct *tsk)
505 struct files_struct *files = tsk->files;
506 const unsigned long *bptr;
509 spin_lock(&files->file_lock);
510 bptr = files->open_fds->fds_bits;
511 count = files->max_fds / (sizeof(unsigned long) * 8);
512 for (total = 0; count > 0; count--) {
514 total += hweight_long(*bptr);
517 spin_unlock(&files->file_lock);
522 * migrate task to new context
523 * gets vxi, puts old_vxi on change
526 int vx_migrate_task(struct task_struct *p, struct vx_info *vxi)
528 struct vx_info *old_vxi;
534 old_vxi = task_get_vx_info(p);
538 vxdprintk(VXD_CBIT(xid, 5),
539 "vx_migrate_task(%p,%p[#%d.%d])", p, vxi,
540 vxi->vx_id, atomic_read(&vxi->vx_usecnt));
542 if (!(ret = vx_migrate_user(p, vxi))) {
546 openfd = vx_openfd_task(p);
549 atomic_dec(&old_vxi->cvirt.nr_threads);
550 atomic_dec(&old_vxi->cvirt.nr_running);
551 atomic_dec(&old_vxi->limit.rcur[RLIMIT_NPROC]);
552 /* FIXME: what about the struct files here? */
553 atomic_sub(openfd, &old_vxi->limit.rcur[VLIMIT_OPENFD]);
555 atomic_inc(&vxi->cvirt.nr_threads);
556 atomic_inc(&vxi->cvirt.nr_running);
557 atomic_inc(&vxi->limit.rcur[RLIMIT_NPROC]);
558 /* FIXME: what about the struct files here? */
559 atomic_add(openfd, &vxi->limit.rcur[VLIMIT_OPENFD]);
562 release_vx_info(old_vxi, p);
563 clr_vx_info(&p->vx_info);
565 claim_vx_info(vxi, p);
566 set_vx_info(&p->vx_info, vxi);
569 vxdprintk(VXD_CBIT(xid, 5),
570 "moved task %p into vxi:%p[#%d]",
577 put_vx_info(old_vxi);
581 int vx_set_init(struct vx_info *vxi, struct task_struct *p)
588 vxdprintk(VXD_CBIT(xid, 6),
589 "vx_set_init(%p[#%d],%p[#%d,%d,%d])",
590 vxi, vxi->vx_id, p, p->xid, p->pid, p->tgid);
592 vxi->vx_initpid = p->tgid;
597 /* vserver syscall commands below here */
599 /* taks xid and vx_info functions */
601 #include <asm/uaccess.h>
604 int vc_task_xid(uint32_t id, void __user *data)
609 struct task_struct *tsk;
611 if (!vx_check(0, VX_ADMIN|VX_WATCH))
614 read_lock(&tasklist_lock);
615 tsk = find_task_by_real_pid(id);
616 xid = (tsk) ? tsk->xid : -ESRCH;
617 read_unlock(&tasklist_lock);
620 xid = vx_current_xid();
625 int vc_vx_info(uint32_t id, void __user *data)
628 struct vcmd_vx_info_v0 vc_data;
630 if (!vx_check(0, VX_ADMIN))
632 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RESOURCE))
635 vxi = locate_vx_info(id);
639 vc_data.xid = vxi->vx_id;
640 vc_data.initpid = vxi->vx_initpid;
643 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
649 /* context functions */
651 int vc_ctx_create(uint32_t xid, void __user *data)
653 struct vx_info *new_vxi;
656 if (!capable(CAP_SYS_ADMIN))
659 if ((xid > MAX_S_CONTEXT) && (xid != VX_DYNAMIC_ID))
665 new_vxi = __create_vx_info(xid);
667 return PTR_ERR(new_vxi);
669 ret = new_vxi->vx_id;
670 vx_migrate_task(current, new_vxi);
671 /* if this fails, we might end up with a hashed vx_info */
672 put_vx_info(new_vxi);
677 int vc_ctx_migrate(uint32_t id, void __user *data)
681 if (!capable(CAP_SYS_ADMIN))
684 /* dirty hack until Spectator becomes a cap */
690 vxi = locate_vx_info(id);
693 vx_migrate_task(current, vxi);
699 int vc_get_cflags(uint32_t id, void __user *data)
702 struct vcmd_ctx_flags_v0 vc_data;
704 if (!capable(CAP_SYS_ADMIN))
707 vxi = locate_vx_info(id);
711 vc_data.flagword = vxi->vx_flags;
713 /* special STATE flag handling */
714 vc_data.mask = vx_mask_flags(~0UL, vxi->vx_flags, VXF_ONE_TIME);
718 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
723 int vc_set_cflags(uint32_t id, void __user *data)
726 struct vcmd_ctx_flags_v0 vc_data;
727 uint64_t mask, trigger;
729 if (!capable(CAP_SYS_ADMIN))
731 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
734 vxi = locate_vx_info(id);
738 /* special STATE flag handling */
739 mask = vx_mask_mask(vc_data.mask, vxi->vx_flags, VXF_ONE_TIME);
740 trigger = (mask & vxi->vx_flags) ^ (mask & vc_data.flagword);
742 if (trigger & VXF_STATE_SETUP)
743 vx_mask_bcaps(current);
744 if (trigger & VXF_STATE_INIT)
745 if (vxi == current->vx_info)
746 vx_set_init(vxi, current);
748 vxi->vx_flags = vx_mask_flags(vxi->vx_flags,
749 vc_data.flagword, mask);
754 int vc_get_ccaps(uint32_t id, void __user *data)
757 struct vcmd_ctx_caps_v0 vc_data;
759 if (!capable(CAP_SYS_ADMIN))
762 vxi = locate_vx_info(id);
766 vc_data.bcaps = vxi->vx_bcaps;
767 vc_data.ccaps = vxi->vx_ccaps;
768 vc_data.cmask = ~0UL;
771 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
776 int vc_set_ccaps(uint32_t id, void __user *data)
779 struct vcmd_ctx_caps_v0 vc_data;
781 if (!capable(CAP_SYS_ADMIN))
783 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
786 vxi = locate_vx_info(id);
790 vxi->vx_bcaps &= vc_data.bcaps;
791 vxi->vx_ccaps = vx_mask_flags(vxi->vx_ccaps,
792 vc_data.ccaps, vc_data.cmask);
797 #include <linux/module.h>
799 EXPORT_SYMBOL_GPL(free_vx_info);