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 vs_context_state(vxi, VS_CONTEXT_DESTROY);
119 namespace = xchg(&vxi->vx_namespace, NULL);
121 put_namespace(namespace);
123 fs = xchg(&vxi->vx_fs, NULL);
130 void free_vx_info(struct vx_info *vxi)
132 /* context shutdown is mandatory */
133 // BUG_ON(vxi->vx_state != VXS_SHUTDOWN);
135 BUG_ON(atomic_read(&vxi->vx_usecnt));
136 BUG_ON(atomic_read(&vxi->vx_tasks));
138 BUG_ON(vx_info_state(vxi, VXS_HASHED));
139 // BUG_ON(!vx_state(vxi, VXS_DEFUNCT));
141 BUG_ON(vxi->vx_namespace);
144 __dealloc_vx_info(vxi);
148 /* hash table for vx_info hash */
150 #define VX_HASH_SIZE 13
152 struct hlist_head vx_info_hash[VX_HASH_SIZE];
154 static spinlock_t vx_info_hash_lock = SPIN_LOCK_UNLOCKED;
157 static inline unsigned int __hashval(xid_t xid)
159 return (xid % VX_HASH_SIZE);
166 * add the vxi to the global hash table
167 * requires the hash_lock to be held */
169 static inline void __hash_vx_info(struct vx_info *vxi)
171 struct hlist_head *head;
173 vxd_assert_lock(&vx_info_hash_lock);
174 vxdprintk(VXD_CBIT(xid, 4),
175 "__hash_vx_info: %p[#%d]", vxi, vxi->vx_id);
176 vxh_hash_vx_info(vxi);
178 /* context must not be hashed */
179 BUG_ON(vxi->vx_state & VXS_HASHED);
182 vxi->vx_state |= VXS_HASHED;
183 head = &vx_info_hash[__hashval(vxi->vx_id)];
184 hlist_add_head(&vxi->vx_hlist, head);
187 /* __unhash_vx_info()
189 * remove the vxi from the global hash table
190 * requires the hash_lock to be held */
192 static inline void __unhash_vx_info(struct vx_info *vxi)
194 vxd_assert_lock(&vx_info_hash_lock);
195 vxdprintk(VXD_CBIT(xid, 4),
196 "__unhash_vx_info: %p[#%d]", vxi, vxi->vx_id);
197 vxh_unhash_vx_info(vxi);
199 /* maybe warn on that? */
200 if (!(vxi->vx_state & VXS_HASHED))
203 vxi->vx_state &= ~VXS_HASHED;
204 hlist_del(&vxi->vx_hlist);
209 /* __lookup_vx_info()
211 * requires the hash_lock to be held
212 * doesn't increment the vx_refcnt */
214 static inline struct vx_info *__lookup_vx_info(xid_t xid)
216 struct hlist_head *head = &vx_info_hash[__hashval(xid)];
217 struct hlist_node *pos;
220 vxd_assert_lock(&vx_info_hash_lock);
221 hlist_for_each(pos, head) {
222 vxi = hlist_entry(pos, struct vx_info, vx_hlist);
224 if (vxi->vx_id == xid)
229 vxdprintk(VXD_CBIT(xid, 0),
230 "__lookup_vx_info(#%u): %p[#%u]",
231 xid, vxi, vxi?vxi->vx_id:0);
232 vxh_lookup_vx_info(xid, vxi);
239 * find unused dynamic xid
240 * requires the hash_lock to be held */
242 static inline xid_t __vx_dynamic_id(void)
244 static xid_t seq = MAX_S_CONTEXT;
247 vxd_assert_lock(&vx_info_hash_lock);
249 if (++seq > MAX_S_CONTEXT)
251 if (!__lookup_vx_info(seq)) {
252 vxdprintk(VXD_CBIT(xid, 4),
253 "__vx_dynamic_id: [#%d]", seq);
256 } while (barrier != seq);
260 #ifdef CONFIG_VSERVER_LEGACY
264 * locate or create the requested context
265 * get() it and if new hash it */
267 static struct vx_info * __loc_vx_info(int id, int *err)
269 struct vx_info *new, *vxi = NULL;
271 vxdprintk(VXD_CBIT(xid, 1), "loc_vx_info(%d)*", id);
273 if (!(new = __alloc_vx_info(id))) {
278 /* required to make dynamic xids unique */
279 spin_lock(&vx_info_hash_lock);
281 /* dynamic context requested */
282 if (id == VX_DYNAMIC_ID) {
283 id = __vx_dynamic_id();
285 printk(KERN_ERR "no dynamic context available.\n");
290 /* existing context requested */
291 else if ((vxi = __lookup_vx_info(id))) {
292 /* context in setup is not available */
293 if (vxi->vx_flags & VXF_STATE_SETUP) {
294 vxdprintk(VXD_CBIT(xid, 0),
295 "loc_vx_info(%d) = %p (not available)", id, vxi);
299 vxdprintk(VXD_CBIT(xid, 0),
300 "loc_vx_info(%d) = %p (found)", id, vxi);
307 /* new context requested */
308 vxdprintk(VXD_CBIT(xid, 0),
309 "loc_vx_info(%d) = %p (new)", id, new);
310 __hash_vx_info(get_vx_info(new));
311 vxi = new, new = NULL;
315 spin_unlock(&vx_info_hash_lock);
316 vxh_loc_vx_info(id, vxi);
318 __dealloc_vx_info(new);
324 /* __create_vx_info()
326 * create the requested context
327 * get() it and hash it */
329 static struct vx_info * __create_vx_info(int id)
331 struct vx_info *new, *vxi = NULL;
333 vxdprintk(VXD_CBIT(xid, 1), "create_vx_info(%d)*", id);
335 if (!(new = __alloc_vx_info(id))) {
336 return ERR_PTR(-ENOMEM);
339 /* required to make dynamic xids unique */
340 spin_lock(&vx_info_hash_lock);
342 /* dynamic context requested */
343 if (id == VX_DYNAMIC_ID) {
344 id = __vx_dynamic_id();
346 printk(KERN_ERR "no dynamic context available.\n");
347 vxi = ERR_PTR(-EAGAIN);
352 /* existing context requested */
353 else if ((vxi = __lookup_vx_info(id))) {
354 vxdprintk(VXD_CBIT(xid, 0),
355 "create_vx_info(%d) = %p (already there)", id, vxi);
356 if (vx_info_flags(vxi, VXF_STATE_SETUP, 0))
357 vxi = ERR_PTR(-EBUSY);
359 vxi = ERR_PTR(-EEXIST);
362 /* dynamic xid creation blocker */
363 else if (id >= MIN_D_CONTEXT) {
364 vxdprintk(VXD_CBIT(xid, 0),
365 "create_vx_info(%d) (dynamic rejected)", id);
366 vxi = ERR_PTR(-EINVAL);
370 /* new context requested */
371 vxdprintk(VXD_CBIT(xid, 0),
372 "create_vx_info(%d) = %p (new)", id, new);
373 __hash_vx_info(get_vx_info(new));
374 vxi = new, new = NULL;
377 spin_unlock(&vx_info_hash_lock);
378 vxh_create_vx_info(id, IS_ERR(vxi)?NULL:vxi);
380 __dealloc_vx_info(new);
388 void unhash_vx_info(struct vx_info *vxi)
390 __shutdown_vx_info(vxi);
391 spin_lock(&vx_info_hash_lock);
392 __unhash_vx_info(vxi);
393 spin_unlock(&vx_info_hash_lock);
399 * search for a vx_info and get() it
400 * negative id means current */
402 struct vx_info *locate_vx_info(int id)
404 struct vx_info *vxi = NULL;
407 vxi = get_vx_info(current->vx_info);
409 spin_lock(&vx_info_hash_lock);
410 vxi = get_vx_info(__lookup_vx_info(id));
411 spin_unlock(&vx_info_hash_lock);
418 * verify that xid is still hashed */
420 int xid_is_hashed(xid_t xid)
424 spin_lock(&vx_info_hash_lock);
425 hashed = (__lookup_vx_info(xid) != NULL);
426 spin_unlock(&vx_info_hash_lock);
430 #ifdef CONFIG_VSERVER_LEGACY
432 struct vx_info *locate_or_create_vx_info(int id)
436 return __loc_vx_info(id, &err);
441 #ifdef CONFIG_PROC_FS
443 int get_xid_list(int index, unsigned int *xids, int size)
445 int hindex, nr_xids = 0;
447 for (hindex = 0; hindex < VX_HASH_SIZE; hindex++) {
448 struct hlist_head *head = &vx_info_hash[hindex];
449 struct hlist_node *pos;
451 spin_lock(&vx_info_hash_lock);
452 hlist_for_each(pos, head) {
458 vxi = hlist_entry(pos, struct vx_info, vx_hlist);
459 xids[nr_xids] = vxi->vx_id;
460 if (++nr_xids >= size) {
461 spin_unlock(&vx_info_hash_lock);
465 /* keep the lock time short */
466 spin_unlock(&vx_info_hash_lock);
473 int vx_migrate_user(struct task_struct *p, struct vx_info *vxi)
475 struct user_struct *new_user, *old_user;
479 new_user = alloc_uid(vxi->vx_id, p->uid);
484 if (new_user != old_user) {
485 atomic_inc(&new_user->processes);
486 atomic_dec(&old_user->processes);
493 void vx_mask_bcaps(struct task_struct *p)
495 struct vx_info *vxi = p->vx_info;
497 p->cap_effective &= vxi->vx_bcaps;
498 p->cap_inheritable &= vxi->vx_bcaps;
499 p->cap_permitted &= vxi->vx_bcaps;
503 #include <linux/file.h>
505 static int vx_openfd_task(struct task_struct *tsk)
507 struct files_struct *files = tsk->files;
508 const unsigned long *bptr;
511 spin_lock(&files->file_lock);
512 bptr = files->open_fds->fds_bits;
513 count = files->max_fds / (sizeof(unsigned long) * 8);
514 for (total = 0; count > 0; count--) {
516 total += hweight_long(*bptr);
519 spin_unlock(&files->file_lock);
524 * migrate task to new context
525 * gets vxi, puts old_vxi on change
528 int vx_migrate_task(struct task_struct *p, struct vx_info *vxi)
530 struct vx_info *old_vxi;
536 old_vxi = task_get_vx_info(p);
540 vxdprintk(VXD_CBIT(xid, 5),
541 "vx_migrate_task(%p,%p[#%d.%d])", p, vxi,
542 vxi->vx_id, atomic_read(&vxi->vx_usecnt));
544 if (!(ret = vx_migrate_user(p, vxi))) {
548 openfd = vx_openfd_task(p);
551 atomic_dec(&old_vxi->cvirt.nr_threads);
552 atomic_dec(&old_vxi->cvirt.nr_running);
553 atomic_dec(&old_vxi->limit.rcur[RLIMIT_NPROC]);
554 /* FIXME: what about the struct files here? */
555 atomic_sub(openfd, &old_vxi->limit.rcur[VLIMIT_OPENFD]);
557 atomic_inc(&vxi->cvirt.nr_threads);
558 atomic_inc(&vxi->cvirt.nr_running);
559 atomic_inc(&vxi->limit.rcur[RLIMIT_NPROC]);
560 /* FIXME: what about the struct files here? */
561 atomic_add(openfd, &vxi->limit.rcur[VLIMIT_OPENFD]);
564 release_vx_info(old_vxi, p);
565 clr_vx_info(&p->vx_info);
567 claim_vx_info(vxi, p);
568 set_vx_info(&p->vx_info, vxi);
571 vxdprintk(VXD_CBIT(xid, 5),
572 "moved task %p into vxi:%p[#%d]",
579 put_vx_info(old_vxi);
583 int vx_set_init(struct vx_info *vxi, struct task_struct *p)
590 vxdprintk(VXD_CBIT(xid, 6),
591 "vx_set_init(%p[#%d],%p[#%d,%d,%d])",
592 vxi, vxi->vx_id, p, p->xid, p->pid, p->tgid);
594 vxi->vx_initpid = p->tgid;
599 /* vserver syscall commands below here */
601 /* taks xid and vx_info functions */
603 #include <asm/uaccess.h>
606 int vc_task_xid(uint32_t id, void __user *data)
611 struct task_struct *tsk;
613 if (!vx_check(0, VX_ADMIN|VX_WATCH))
616 read_lock(&tasklist_lock);
617 tsk = find_task_by_real_pid(id);
618 xid = (tsk) ? tsk->xid : -ESRCH;
619 read_unlock(&tasklist_lock);
622 xid = vx_current_xid();
627 int vc_vx_info(uint32_t id, void __user *data)
630 struct vcmd_vx_info_v0 vc_data;
632 if (!vx_check(0, VX_ADMIN))
634 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RESOURCE))
637 vxi = locate_vx_info(id);
641 vc_data.xid = vxi->vx_id;
642 vc_data.initpid = vxi->vx_initpid;
645 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
651 /* context functions */
653 int vc_ctx_create(uint32_t xid, void __user *data)
655 struct vx_info *new_vxi;
658 if (!capable(CAP_SYS_ADMIN))
661 if ((xid > MAX_S_CONTEXT) && (xid != VX_DYNAMIC_ID))
667 new_vxi = __create_vx_info(xid);
669 return PTR_ERR(new_vxi);
671 vs_context_state(new_vxi, VS_CONTEXT_CREATED);
672 ret = new_vxi->vx_id;
673 vx_migrate_task(current, new_vxi);
674 /* if this fails, we might end up with a hashed vx_info */
675 put_vx_info(new_vxi);
680 int vc_ctx_migrate(uint32_t id, void __user *data)
684 if (!capable(CAP_SYS_ADMIN))
687 /* dirty hack until Spectator becomes a cap */
693 vxi = locate_vx_info(id);
696 vx_migrate_task(current, vxi);
702 int vc_get_cflags(uint32_t id, void __user *data)
705 struct vcmd_ctx_flags_v0 vc_data;
707 if (!capable(CAP_SYS_ADMIN))
710 vxi = locate_vx_info(id);
714 vc_data.flagword = vxi->vx_flags;
716 /* special STATE flag handling */
717 vc_data.mask = vx_mask_flags(~0UL, vxi->vx_flags, VXF_ONE_TIME);
721 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
726 int vc_set_cflags(uint32_t id, void __user *data)
729 struct vcmd_ctx_flags_v0 vc_data;
730 uint64_t mask, trigger;
732 if (!capable(CAP_SYS_ADMIN))
734 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
737 vxi = locate_vx_info(id);
741 /* special STATE flag handling */
742 mask = vx_mask_mask(vc_data.mask, vxi->vx_flags, VXF_ONE_TIME);
743 trigger = (mask & vxi->vx_flags) ^ (mask & vc_data.flagword);
745 if (trigger & VXF_STATE_SETUP)
746 vx_mask_bcaps(current);
747 if (trigger & VXF_STATE_INIT)
748 if (vxi == current->vx_info)
749 vx_set_init(vxi, current);
751 vxi->vx_flags = vx_mask_flags(vxi->vx_flags,
752 vc_data.flagword, mask);
757 int vc_get_ccaps(uint32_t id, void __user *data)
760 struct vcmd_ctx_caps_v0 vc_data;
762 if (!capable(CAP_SYS_ADMIN))
765 vxi = locate_vx_info(id);
769 vc_data.bcaps = vxi->vx_bcaps;
770 vc_data.ccaps = vxi->vx_ccaps;
771 vc_data.cmask = ~0UL;
774 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
779 int vc_set_ccaps(uint32_t id, void __user *data)
782 struct vcmd_ctx_caps_v0 vc_data;
784 if (!capable(CAP_SYS_ADMIN))
786 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
789 vxi = locate_vx_info(id);
793 vxi->vx_bcaps &= vc_data.bcaps;
794 vxi->vx_ccaps = vx_mask_flags(vxi->vx_ccaps,
795 vc_data.ccaps, vc_data.cmask);
800 #include <linux/module.h>
802 EXPORT_SYMBOL_GPL(free_vx_info);