From: Dhaval Giani <dhaval@linux.vnet.ibm.com>
To: Ingo Molnar <mingo@elte.hu>
Cc: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>,
Mike Galbraith <efault@gmx.de>,
Peter Zijlstra <a.p.zijlstra@chello.nl>,
Dmitry Adamushko <dmitry.adamushko@gmail.com>,
lkml <linux-kernel@vger.kernel.org>,
maneesh@linux.vnet.ibm.com,
Andrew Morton <akpm@linux-foundation.org>,
Sudhir Kumar <skumar@linux.vnet.ibm.com>
Subject: [RFC/PATCH] Add sysfs control to modify a user's cpu share
Date: Mon, 1 Oct 2007 19:34:54 +0530 [thread overview]
Message-ID: <20071001140454.GA19439@linux.vnet.ibm.com> (raw)
In-Reply-To: <20070926210737.GA8663@elte.hu>
Hi Ingo,
Adds tunables in sysfs to modify a user's cpu share.
A directory is created in sysfs for each new user in the system.
/sys/kernel/uids/<uid>/cpu_share
Reading this file returns the cpu shares granted for the user.
Writing into this file modifies the cpu share for the user. Only an
administrator is allowed to modify a user's cpu share.
Ex:
# cd /sys/kernel/uids/
# cat 512/cpu_share
1024
# echo 2048 > 512/cpu_share
# cat 512/cpu_share
2048
#
Signed-off-by: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
Signed-off-by: Dhaval Giani <dhaval@linux.vnet.ibm.com>
---
include/linux/sched.h | 11 +++++
kernel/ksysfs.c | 4 +
kernel/sched.c | 5 ++
kernel/user.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++-
4 files changed, 129 insertions(+), 1 deletion(-)
Index: linux-2.6.23-rc8-sched-devel/include/linux/sched.h
===================================================================
--- linux-2.6.23-rc8-sched-devel.orig/include/linux/sched.h
+++ linux-2.6.23-rc8-sched-devel/include/linux/sched.h
@@ -86,6 +86,7 @@ struct sched_param {
#include <linux/timer.h>
#include <linux/hrtimer.h>
#include <linux/task_io_accounting.h>
+#include <linux/kobject.h>
#include <asm/processor.h>
@@ -598,9 +599,18 @@ struct user_struct {
#ifdef CONFIG_FAIR_USER_SCHED
struct task_group *tg;
+ struct kset kset;
+ struct subsys_attribute user_attr;
+ struct work_struct work;
#endif
};
+#ifdef CONFIG_FAIR_USER_SCHED
+extern int uids_kobject_init(void);
+#else
+static inline int uids_kobject_init(void) { return 0; }
+#endif
+
extern struct user_struct *find_user(uid_t);
extern struct user_struct root_user;
@@ -1846,6 +1856,7 @@ extern struct task_group *sched_create_g
extern void sched_destroy_group(struct task_group *tg);
extern void sched_move_task(struct task_struct *tsk);
extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
+extern unsigned long sched_group_shares(struct task_group *tg);
#endif
Index: linux-2.6.23-rc8-sched-devel/kernel/ksysfs.c
===================================================================
--- linux-2.6.23-rc8-sched-devel.orig/kernel/ksysfs.c
+++ linux-2.6.23-rc8-sched-devel/kernel/ksysfs.c
@@ -14,6 +14,7 @@
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kexec.h>
+#include <linux/sched.h>
#define KERNEL_ATTR_RO(_name) \
static struct subsys_attribute _name##_attr = __ATTR_RO(_name)
@@ -116,6 +117,9 @@ static int __init ksysfs_init(void)
¬es_attr);
}
+ if (!error)
+ error = uids_kobject_init();
+
return error;
}
Index: linux-2.6.23-rc8-sched-devel/kernel/sched.c
===================================================================
--- linux-2.6.23-rc8-sched-devel.orig/kernel/sched.c
+++ linux-2.6.23-rc8-sched-devel/kernel/sched.c
@@ -6928,4 +6928,9 @@ int sched_group_set_shares(struct task_g
return 0;
}
+unsigned long sched_group_shares(struct task_group *tg)
+{
+ return tg->shares;
+}
+
#endif /* CONFIG_FAIR_GROUP_SCHED */
Index: linux-2.6.23-rc8-sched-devel/kernel/user.c
===================================================================
--- linux-2.6.23-rc8-sched-devel.orig/kernel/user.c
+++ linux-2.6.23-rc8-sched-devel/kernel/user.c
@@ -56,6 +56,62 @@ struct user_struct root_user = {
};
#ifdef CONFIG_FAIR_USER_SCHED
+
+static struct kobject uids_kobject;
+
+ssize_t cpu_shares_show(struct kset *kset, char *buffer)
+{
+ struct user_struct *up = container_of(kset, struct user_struct, kset);
+
+ return sprintf(buffer, "%lu\n", sched_group_shares(up->tg));
+}
+
+ssize_t cpu_shares_store(struct kset *kset, const char *buffer, size_t size)
+{
+ struct user_struct *up = container_of(kset, struct user_struct, kset);
+ unsigned long shares;
+ int rc;
+
+ sscanf(buffer, "%lu", &shares);
+
+ rc = sched_group_set_shares(up->tg, shares);
+
+ return (rc ? rc : size);
+}
+
+static void user_attr_init(struct subsys_attribute *sa, char *name, int mode)
+{
+ sa->attr.name = name;
+ sa->attr.mode = mode;
+ sa->show = cpu_shares_show;
+ sa->store = cpu_shares_store;
+}
+
+static int user_kobject_create(struct user_struct *up)
+{
+ struct kset *kset = &up->kset;
+ struct kobject *kobj = &kset->kobj;
+ int error;
+
+ memset(kset, 0, sizeof(struct kset));
+ kobj->parent = &uids_kobject;
+ kobject_set_name(kobj, "%d", up->uid);
+ kset_init(kset);
+ user_attr_init(&up->user_attr, "cpu_share", 0644);
+
+ error = kobject_add(kobj);
+
+ if (error)
+ goto done;
+
+ error = sysfs_create_file(kobj, &up->user_attr.attr);
+ if (error)
+ kobject_del(kobj);
+
+done:
+ return error;
+}
+
static void sched_destroy_user(struct user_struct *up)
{
sched_destroy_group(up->tg);
@@ -77,11 +133,53 @@ static void sched_switch_user(struct tas
sched_move_task(p);
}
+int __init uids_kobject_init(void)
+{
+ int error;
+
+ uids_kobject.parent = &kernel_subsys.kobj;
+ kobject_set_name(&uids_kobject, "uids");
+ kobject_init(&uids_kobject);
+ error = kobject_add(&uids_kobject);
+
+ if (!error)
+ error = user_kobject_create(&root_user);
+
+ return error;
+}
+
+static void remove_user_sysfs_dir(struct work_struct *w)
+{
+ struct user_struct *up = container_of(w, struct user_struct, work);
+ struct kobject *kobj = &up->kset.kobj;
+
+ sysfs_remove_file(kobj, &up->user_attr.attr);
+ kobject_del(kobj);
+ kmem_cache_free(uid_cachep, up);
+}
+
+static inline void free_user(struct user_struct *up)
+{
+ struct kobject *kobj = &up->kset.kobj;
+
+ if (!kobj->sd)
+ return;
+
+ INIT_WORK(&up->work, remove_user_sysfs_dir);
+ schedule_work(&up->work);
+}
+
#else /* CONFIG_FAIR_USER_SCHED */
static void sched_destroy_user(struct user_struct *up) { }
static int sched_create_user(struct user_struct *up) { return 0; }
static void sched_switch_user(struct task_struct *p) { }
+static inline int user_kobject_create(struct user_struct *up) { return 0; }
+
+static inline void free_user(struct user_struct *up)
+{
+ kmem_cache_free(uid_cachep, up);
+}
#endif /* CONFIG_FAIR_USER_SCHED */
@@ -145,7 +243,7 @@ void free_uid(struct user_struct *up)
sched_destroy_user(up);
key_put(up->uid_keyring);
key_put(up->session_keyring);
- kmem_cache_free(uid_cachep, up);
+ free_user(up);
} else {
local_irq_restore(flags);
}
@@ -155,6 +253,7 @@ struct user_struct * alloc_uid(struct us
{
struct hlist_head *hashent = uidhashentry(ns, uid);
struct user_struct *up;
+ int create_sysfs_dir = 0;
spin_lock_irq(&uidhash_lock);
up = uid_hash_find(uid, hashent);
@@ -205,10 +304,19 @@ struct user_struct * alloc_uid(struct us
} else {
uid_hash_insert(new, hashent);
up = new;
+ create_sysfs_dir = 1;
}
spin_unlock_irq(&uidhash_lock);
}
+
+ if (create_sysfs_dir) {
+ if (user_kobject_create(up)) {
+ free_uid(up);
+ up = NULL;
+ }
+ }
+
return up;
}
--
regards,
Dhaval
next prev parent reply other threads:[~2007-10-01 14:09 UTC|newest]
Thread overview: 53+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-09-24 21:45 [git] CFS-devel, latest code Ingo Molnar
2007-09-24 21:55 ` Andrew Morton
2007-09-24 21:59 ` Ingo Molnar
2007-09-25 0:08 ` Daniel Walker
2007-09-25 6:45 ` Ingo Molnar
2007-09-25 15:17 ` Daniel Walker
2007-09-25 6:10 ` Mike Galbraith
2007-09-25 7:35 ` Mike Galbraith
2007-09-25 8:33 ` Mike Galbraith
2007-09-25 8:53 ` Srivatsa Vaddagiri
2007-09-25 9:11 ` Srivatsa Vaddagiri
2007-09-25 9:15 ` Mike Galbraith
2007-09-25 9:12 ` Mike Galbraith
2007-09-25 9:13 ` Ingo Molnar
2007-09-25 9:17 ` Mike Galbraith
2007-09-25 9:47 ` Ingo Molnar
2007-09-25 10:02 ` Mike Galbraith
2007-09-26 8:04 ` Mike Galbraith
2007-09-28 21:46 ` Bill Davidsen
2007-09-25 9:44 ` Srivatsa Vaddagiri
2007-09-25 9:40 ` Ingo Molnar
2007-09-25 10:10 ` Ingo Molnar
2007-09-25 10:28 ` Srivatsa Vaddagiri
2007-09-25 10:36 ` Ingo Molnar
2007-09-25 11:33 ` Ingo Molnar
2007-09-25 14:48 ` Srivatsa Vaddagiri
2007-09-25 12:51 ` Srivatsa Vaddagiri
2007-09-25 13:35 ` Mike Galbraith
2007-09-25 14:07 ` Srivatsa Vaddagiri
2007-09-25 12:28 ` Mike Galbraith
2007-09-25 12:54 ` Mike Galbraith
[not found] ` <20070925131717.GM26289@linux.vnet.ibm.com>
[not found] ` <1190725693.13716.10.camel@Homer.simpson.net>
[not found] ` <20070925132528.GN26289@linux.vnet.ibm.com>
[not found] ` <1190726682.11260.1.camel@Homer.simpson.net>
[not found] ` <20070925140559.GB26310@linux.vnet.ibm.com>
[not found] ` <20070925143755.GA15594@elte.hu>
[not found] ` <20070926210737.GA8663@elte.hu>
2007-10-01 14:04 ` Dhaval Giani [this message]
2007-10-01 14:44 ` [RFC/PATCH] Add sysfs control to modify a user's cpu share Ingo Molnar
2007-10-01 15:32 ` Srivatsa Vaddagiri
2007-10-02 22:12 ` Eric St-Laurent
2007-10-03 4:09 ` Srivatsa Vaddagiri
2007-10-03 17:10 ` [RFC/PATCH -v2] " Dhaval Giani
2007-10-04 7:57 ` Ingo Molnar
2007-10-04 8:54 ` Heiko Carstens
2007-10-04 16:02 ` Bill Davidsen
2007-10-04 17:20 ` Srivatsa Vaddagiri
2007-10-04 21:32 ` Valdis.Kletnieks
2007-10-05 7:01 ` Srivatsa Vaddagiri
2007-10-09 15:12 ` [PATCH sched-devel] Generate uevents for user creation/destruction Srivatsa Vaddagiri
2007-10-10 7:42 ` Ingo Molnar
2007-10-01 16:12 ` [RFC/PATCH] Add sysfs control to modify a user's cpu share Dave Jones
2007-10-01 16:37 ` Srivatsa Vaddagiri
2007-09-25 6:50 ` [git] CFS-devel, latest code S.Çağlar Onur
2007-09-25 9:17 ` Ingo Molnar
2007-09-25 7:41 ` Andrew Morton
2007-09-25 8:43 ` Srivatsa Vaddagiri
2007-09-25 8:48 ` Andrew Morton
2007-09-25 11:00 ` Ingo Molnar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20071001140454.GA19439@linux.vnet.ibm.com \
--to=dhaval@linux.vnet.ibm.com \
--cc=a.p.zijlstra@chello.nl \
--cc=akpm@linux-foundation.org \
--cc=dmitry.adamushko@gmail.com \
--cc=efault@gmx.de \
--cc=linux-kernel@vger.kernel.org \
--cc=maneesh@linux.vnet.ibm.com \
--cc=mingo@elte.hu \
--cc=skumar@linux.vnet.ibm.com \
--cc=vatsa@linux.vnet.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.