netns xfrm: fix "ip xfrm state|policy count" misreport
[safe/jmp/linux-2.6] / kernel / user.c
index 6c924bc..46d0165 100644 (file)
@@ -20,7 +20,7 @@
 
 struct user_namespace init_user_ns = {
        .kref = {
-               .refcount       = ATOMIC_INIT(1),
+               .refcount       = ATOMIC_INIT(2),
        },
        .creator = &root_user,
 };
@@ -72,21 +72,7 @@ static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
 static void uid_hash_remove(struct user_struct *up)
 {
        hlist_del_init(&up->uidhash_node);
-}
-
-static struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent)
-{
-       struct user_struct *user;
-       struct hlist_node *h;
-
-       hlist_for_each_entry(user, h, hashent, uidhash_node) {
-               if (user->uid == uid) {
-                       atomic_inc(&user->__count);
-                       return user;
-               }
-       }
-
-       return NULL;
+       put_user_ns(up->user_ns);
 }
 
 #ifdef CONFIG_USER_SCHED
@@ -104,6 +90,8 @@ static int sched_create_user(struct user_struct *up)
        if (IS_ERR(up->tg))
                rc = -ENOMEM;
 
+       set_tg_uid(up);
+
        return rc;
 }
 
@@ -116,6 +104,23 @@ static int sched_create_user(struct user_struct *up) { return 0; }
 
 #if defined(CONFIG_USER_SCHED) && defined(CONFIG_SYSFS)
 
+static struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent)
+{
+       struct user_struct *user;
+       struct hlist_node *h;
+
+       hlist_for_each_entry(user, h, hashent, uidhash_node) {
+               if (user->uid == uid) {
+                       /* possibly resurrect an "almost deleted" object */
+                       if (atomic_inc_return(&user->__count) == 1)
+                               cancel_delayed_work(&user->work);
+                       return user;
+               }
+       }
+
+       return NULL;
+}
+
 static struct kset *uids_kset; /* represents the /sys/kernel/uids/ directory */
 static DEFINE_MUTEX(uids_mutex);
 
@@ -239,7 +244,13 @@ static struct kobj_type uids_ktype = {
        .release = uids_release,
 };
 
-/* create /sys/kernel/uids/<uid>/cpu_share file for this user */
+/*
+ * Create /sys/kernel/uids/<uid>/cpu_share file for this user
+ * We do not create this file for users in a user namespace (until
+ * sysfs tagging is implemented).
+ *
+ * See Documentation/scheduler/sched-design-CFS.txt for ramifications.
+ */
 static int uids_user_create(struct user_struct *up)
 {
        struct kobject *kobj = &up->kobj;
@@ -274,38 +285,35 @@ int __init uids_sysfs_init(void)
        return uids_user_create(&root_user);
 }
 
-/* work function to remove sysfs directory for a user and free up
+/* delayed work function to remove sysfs directory for a user and free up
  * corresponding structures.
  */
-static void remove_user_sysfs_dir(struct work_struct *w)
+static void cleanup_user_struct(struct work_struct *w)
 {
-       struct user_struct *up = container_of(w, struct user_struct, work);
+       struct user_struct *up = container_of(w, struct user_struct, work.work);
        unsigned long flags;
        int remove_user = 0;
 
-       if (up->user_ns != &init_user_ns)
-               return;
        /* Make uid_hash_remove() + sysfs_remove_file() + kobject_del()
         * atomic.
         */
        uids_mutex_lock();
 
-       local_irq_save(flags);
-
-       if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
+       spin_lock_irqsave(&uidhash_lock, flags);
+       if (atomic_read(&up->__count) == 0) {
                uid_hash_remove(up);
                remove_user = 1;
-               spin_unlock_irqrestore(&uidhash_lock, flags);
-       } else {
-               local_irq_restore(flags);
        }
+       spin_unlock_irqrestore(&uidhash_lock, flags);
 
        if (!remove_user)
                goto done;
 
-       kobject_uevent(&up->kobj, KOBJ_REMOVE);
-       kobject_del(&up->kobj);
-       kobject_put(&up->kobj);
+       if (up->user_ns == &init_user_ns) {
+               kobject_uevent(&up->kobj, KOBJ_REMOVE);
+               kobject_del(&up->kobj);
+               kobject_put(&up->kobj);
+       }
 
        sched_destroy_user(up);
        key_put(up->uid_keyring);
@@ -322,17 +330,28 @@ done:
  */
 static void free_user(struct user_struct *up, unsigned long flags)
 {
-       /* restore back the count */
-       atomic_inc(&up->__count);
+       INIT_DELAYED_WORK(&up->work, cleanup_user_struct);
+       schedule_delayed_work(&up->work, msecs_to_jiffies(1000));
        spin_unlock_irqrestore(&uidhash_lock, flags);
-
-       put_user_ns(up->user_ns);
-       INIT_WORK(&up->work, remove_user_sysfs_dir);
-       schedule_work(&up->work);
 }
 
 #else  /* CONFIG_USER_SCHED && CONFIG_SYSFS */
 
+static struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent)
+{
+       struct user_struct *user;
+       struct hlist_node *h;
+
+       hlist_for_each_entry(user, h, hashent, uidhash_node) {
+               if (user->uid == uid) {
+                       atomic_inc(&user->__count);
+                       return user;
+               }
+       }
+
+       return NULL;
+}
+
 int uids_sysfs_init(void) { return 0; }
 static inline int uids_user_create(struct user_struct *up) { return 0; }
 static inline void uids_mutex_lock(void) { }
@@ -349,12 +368,29 @@ static void free_user(struct user_struct *up, unsigned long flags)
        sched_destroy_user(up);
        key_put(up->uid_keyring);
        key_put(up->session_keyring);
-       put_user_ns(up->user_ns);
        kmem_cache_free(uid_cachep, up);
 }
 
 #endif
 
+#if defined(CONFIG_RT_GROUP_SCHED) && defined(CONFIG_USER_SCHED)
+/*
+ * We need to check if a setuid can take place. This function should be called
+ * before successfully completing the setuid.
+ */
+int task_can_switch_user(struct user_struct *up, struct task_struct *tsk)
+{
+
+       return sched_rt_can_attach(up->tg, tsk);
+
+}
+#else
+int task_can_switch_user(struct user_struct *up, struct task_struct *tsk)
+{
+       return 1;
+}
+#endif
+
 /*
  * Locate the user_struct for the passed UID.  If found, take a ref on it.  The
  * caller must undo that ref with free_uid().