aboutsummaryrefslogtreecommitdiff
path: root/kernel/cgroup_timer_slack.c
blob: 2817e22a5e82299673fd2e837b57dd7209463692 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * cgroup_timer_slack.c - control group timer slack subsystem
 *
 * Copyright Nokia Corparation, 2011
 * Author: Kirill A. Shutemov
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
#include <linux/cgroup.h>
#include <linux/slab.h>
#include <linux/err.h>

struct cgroup_subsys timer_slack_subsys;
struct tslack_cgroup {
	struct cgroup_subsys_state css;
	unsigned long min_slack_ns;
};

static struct tslack_cgroup *cgroup_to_tslack(struct cgroup *cgroup)
{
	struct cgroup_subsys_state *css;

	css = cgroup_subsys_state(cgroup, timer_slack_subsys.subsys_id);
	return container_of(css, struct tslack_cgroup, css);
}

static struct cgroup_subsys_state *tslack_create(struct cgroup *cgroup)
{
	struct tslack_cgroup *tslack_cgroup;

	tslack_cgroup = kmalloc(sizeof(*tslack_cgroup), GFP_KERNEL);
	if (!tslack_cgroup)
		return ERR_PTR(-ENOMEM);

	if (cgroup->parent) {
		struct tslack_cgroup *parent;

		parent = cgroup_to_tslack(cgroup->parent);
		tslack_cgroup->min_slack_ns = parent->min_slack_ns;
	} else
		tslack_cgroup->min_slack_ns = 0UL;

	return &tslack_cgroup->css;
}

static void tslack_destroy(struct cgroup *cgroup)
{
	kfree(cgroup_to_tslack(cgroup));
}

static u64 tslack_read_min(struct cgroup *cgroup, struct cftype *cft)
{
	return cgroup_to_tslack(cgroup)->min_slack_ns;
}

static int tslack_write_min(struct cgroup *cgroup, struct cftype *cft, u64 val)
{
	if (val > ULONG_MAX)
		return -EINVAL;

	cgroup_to_tslack(cgroup)->min_slack_ns = val;

	return 0;
}

static u64 tslack_read_effective(struct cgroup *cgroup, struct cftype *cft)
{
	unsigned long min;

	min = cgroup_to_tslack(cgroup)->min_slack_ns;
	while (cgroup->parent) {
		cgroup = cgroup->parent;
		min = max(cgroup_to_tslack(cgroup)->min_slack_ns, min);
	}

	return min;
}

static struct cftype files[] = {
	{
		.name = "min_slack_ns",
		.read_u64 = tslack_read_min,
		.write_u64 = tslack_write_min,
	},
	{
		.name = "effective_slack_ns",
		.read_u64 = tslack_read_effective,
	},
};

static int tslack_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
{
	return cgroup_add_files(cgroup, subsys, files, ARRAY_SIZE(files));
}

static int tslack_allow_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
{
	const struct cred *cred = current_cred(), *tcred;
	struct task_struct *task;

	cgroup_taskset_for_each(task, cgrp, tset) {
		tcred = __task_cred(task);

		if ((current != task) && !capable(CAP_SYS_NICE) &&
		    cred->euid != tcred->uid && cred->euid != tcred->suid)
			return -EACCES;
	}

	return 0;
}

struct cgroup_subsys timer_slack_subsys = {
	.name		= "timer_slack",
	.subsys_id	= timer_slack_subsys_id,
	.create		= tslack_create,
	.destroy	= tslack_destroy,
	.populate	= tslack_populate,
	.allow_attach	= tslack_allow_attach,
};

unsigned long task_get_effective_timer_slack(struct task_struct *tsk)
{
	struct cgroup *cgroup;
	unsigned long slack;

	rcu_read_lock();
	cgroup = task_cgroup(tsk, timer_slack_subsys.subsys_id);
	slack = tslack_read_effective(cgroup, NULL);
	rcu_read_unlock();

	return max(tsk->timer_slack_ns, slack);
}