lowmem: sysfs has changed
[pandora-kernel.git] / security / lowmem.c
1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/mman.h>
4 #include <linux/init.h>
5 #include <linux/security.h>
6 #include <linux/sysctl.h>
7 #include <linux/swap.h>
8 #include <linux/kobject.h>
9 #include <linux/pagemap.h>
10 #include <linux/hugetlb.h>
11 #include <linux/sysfs.h>
12 #include <linux/oom.h>
13
14 #define MY_NAME "lowmem"
15
16 #define LOWMEM_MAX_UIDS 8
17
18 enum {
19         VM_LOWMEM_DENY_PAGES = 1,
20         VM_LOWMEM_NOTIFY_LOW_PAGES,
21         VM_LOWMEM_NOTIFY_HIGH_PAGES,
22         VM_LOWMEM_NR_DECAY_PAGES,
23         VM_LOWMEM_ALLOWED_UIDS,
24         VM_LOWMEM_ALLOWED_PAGES,
25         VM_LOWMEM_FREE_PAGES,
26 };
27
28 static long deny_pages;
29 static long notify_low_pages, notify_high_pages;
30 static unsigned int nr_decay_pages;
31 static unsigned long allowed_pages;
32 static unsigned long lowmem_free_pages;
33 static unsigned int allowed_uids[LOWMEM_MAX_UIDS];
34 static unsigned int minuid = 1;
35 static unsigned int maxuid = 65535;
36
37 static ctl_table lowmem_table[] = {
38         {
39                 .ctl_name = VM_LOWMEM_DENY_PAGES,
40                 .procname = "lowmem_deny_watermark_pages",
41                 .data = &deny_pages,
42                 .maxlen = sizeof(long),
43                 .mode = 0644,
44                 .child = NULL,
45                 .proc_handler = &proc_dointvec,
46                 .strategy = &sysctl_intvec,
47         }, {
48                 .ctl_name = VM_LOWMEM_NOTIFY_LOW_PAGES,
49                 .procname = "lowmem_notify_low_pages",
50                 .data = &notify_low_pages,
51                 .maxlen = sizeof(long),
52                 .mode = 0644,
53                 .child = NULL,
54                 .proc_handler = &proc_dointvec,
55                 .strategy = &sysctl_intvec,
56         }, {
57                 .ctl_name = VM_LOWMEM_NOTIFY_HIGH_PAGES,
58                 .procname = "lowmem_notify_high_pages",
59                 .data = &notify_high_pages,
60                 .maxlen = sizeof(long),
61                 .mode = 0644,
62                 .child = NULL,
63                 .proc_handler = &proc_dointvec,
64                 .strategy = &sysctl_intvec,
65         }, {
66                 .ctl_name = VM_LOWMEM_NR_DECAY_PAGES,
67                 .procname = "lowmem_nr_decay_pages",
68                 .data = &nr_decay_pages,
69                 .maxlen = sizeof(unsigned int),
70                 .mode = 0644,
71                 .child = NULL,
72                 .proc_handler = &proc_dointvec,
73                 .strategy = &sysctl_intvec,
74         }, {
75                 .ctl_name = VM_LOWMEM_ALLOWED_UIDS,
76                 .procname = "lowmem_allowed_uids",
77                 .data = &allowed_uids,
78                 .maxlen = LOWMEM_MAX_UIDS * sizeof(unsigned int),
79                 .mode = 0644,
80                 .child = NULL,
81                 .proc_handler = &proc_dointvec_minmax,
82                 .strategy = &sysctl_intvec,
83                 .extra1 = &minuid,
84                 .extra2 = &maxuid,
85         }, {
86                 .ctl_name = VM_LOWMEM_ALLOWED_PAGES,
87                 .procname = "lowmem_allowed_pages",
88                 .data = &allowed_pages,
89                 .maxlen = sizeof(unsigned long),
90                 .mode = 0444,
91                 .child = NULL,
92                 .proc_handler = &proc_dointvec,
93                 .strategy = &sysctl_intvec,
94         }, {
95                 .ctl_name = VM_LOWMEM_FREE_PAGES,
96                 .procname = "lowmem_free_pages",
97                 .data = &lowmem_free_pages,
98                 .maxlen = sizeof(unsigned long),
99                 .mode = 0444,
100                 .child = NULL,
101                 .proc_handler = &proc_dointvec,
102                 .strategy = &sysctl_intvec,
103         }, {
104                 .ctl_name = 0
105         }
106 };
107
108 static ctl_table lowmem_root_table[] = {
109         {
110                 .ctl_name = CTL_VM,
111                 .procname = "vm",
112                 .mode = 0555,
113                 .child = lowmem_table,
114         }, {
115                 .ctl_name = 0
116         }
117 };
118
119 #define KERNEL_ATTR_RO(_name) \
120 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
121
122 static int low_watermark_reached, high_watermark_reached;
123
124 static ssize_t low_watermark_show(struct kobject *kobj,
125                                   struct kobj_attribute *attr, char *page)
126 {
127         return sprintf(page, "%u\n", low_watermark_reached);
128 }
129
130 static ssize_t high_watermark_show(struct kobject *kobj,
131                                    struct kobj_attribute *attr, char *page)
132 {
133         return sprintf(page, "%u\n", high_watermark_reached);
134 }
135
136 KERNEL_ATTR_RO(low_watermark);
137 KERNEL_ATTR_RO(high_watermark);
138
139 static void low_watermark_state(int new_state)
140 {
141         if (low_watermark_reached != new_state) {
142                 low_watermark_reached = new_state;
143                 sysfs_notify(kernel_kobj, NULL, "low_watermark");
144         }
145 }
146
147 static void high_watermark_state(int new_state)
148 {
149         if (high_watermark_reached != new_state) {
150                 high_watermark_reached = new_state;
151                 sysfs_notify(kernel_kobj, NULL, "high_watermark");
152         }
153 }
154
155 static int low_vm_enough_memory(long pages)
156 {
157         unsigned long free, allowed;
158         int cap_sys_admin = 0, notify;
159
160         if (cap_capable(current, CAP_SYS_ADMIN) == 0)
161                 cap_sys_admin = 1;
162
163         allowed = totalram_pages - hugetlb_total_pages();
164         allowed_pages = allowed;
165
166         /* We activate ourselves only after both parameters have been
167          * configured. */
168         if (deny_pages == 0 || notify_low_pages == 0 || notify_high_pages == 0)
169                 return  __vm_enough_memory(pages, cap_sys_admin);
170
171         vm_acct_memory(pages);
172
173         /* Easily freed pages when under VM pressure or direct reclaim */
174         free = global_page_state(NR_FILE_PAGES);
175         free += nr_swap_pages;
176         free += global_page_state(NR_SLAB_RECLAIMABLE);
177
178         if (likely(free > notify_low_pages))
179                 goto enough_memory;
180
181         /* No luck, lets make it more expensive and try again.. */
182         free += nr_free_pages();
183
184         if (free < deny_pages) {
185                 int i;
186
187                 lowmem_free_pages = free;
188                 low_watermark_state(1);
189                 high_watermark_state(1);
190                 /* Memory allocations by root are always allowed */
191                 if (cap_sys_admin)
192                         return 0;
193
194                 /* OOM unkillable process is allowed to consume memory */
195                 if (current->oomkilladj == OOM_DISABLE)
196                         return 0;
197
198                 /* uids from allowed_uids vector are also allowed no matter what */
199                 for (i = 0; i < LOWMEM_MAX_UIDS && allowed_uids[i]; i++)
200                         if (current->uid == allowed_uids[i])
201                                 return 0;
202
203                 vm_unacct_memory(pages);
204                 if (printk_ratelimit()) {
205                         printk(MY_NAME ": denying memory allocation to process %d (%s)\n",
206                                current->pid, current->comm);
207                 }
208                 return -ENOMEM;
209         }
210
211 enough_memory:
212         /* See if we need to notify level 1 */
213         low_watermark_state(free < notify_low_pages);
214
215         /*
216          * In the level 2 notification case things are more complicated,
217          * as the level that we drop the state and send a notification
218          * should be lower than when it is first triggered. Having this
219          * on the same watermark level ends up bouncing back and forth
220          * when applications are being stupid.
221          */
222         notify = free < notify_high_pages;
223         if (notify || free - nr_decay_pages > notify_high_pages)
224                 high_watermark_state(notify);
225
226         /* We have plenty of memory */
227         lowmem_free_pages = free;
228         return 0;
229 }
230
231 static struct security_operations lowmem_security_ops = {
232         /* Use the capability functions for some of the hooks */
233         .ptrace = cap_ptrace,
234         .capget = cap_capget,
235         .capset_check = cap_capset_check,
236         .capset_set = cap_capset_set,
237         .capable = cap_capable,
238
239         .bprm_apply_creds = cap_bprm_apply_creds,
240         .bprm_set_security = cap_bprm_set_security,
241
242         .task_post_setuid = cap_task_post_setuid,
243         .task_reparent_to_init = cap_task_reparent_to_init,
244         .vm_enough_memory = low_vm_enough_memory,
245 };
246
247 static struct ctl_table_header *lowmem_table_header;
248 /* flag to keep track of how we were registered */
249 static int secondary;
250
251 static struct attribute *lowmem_attrs[] = {
252         &low_watermark_attr.attr,
253         &high_watermark_attr.attr,
254         NULL,
255 };
256
257 static struct attribute_group lowmem_attr_group = {
258         .attrs  = lowmem_attrs,
259 };
260
261 static int __init lowmem_init(void)
262 {
263         int r;
264
265         /* register ourselves with the security framework */
266         if (register_security(&lowmem_security_ops)) {
267                 printk(KERN_ERR MY_NAME ": Failure registering with the kernel\n");
268                 /* try registering with primary module */
269                 if (mod_reg_security(MY_NAME, &lowmem_security_ops)) {
270                         printk(KERN_ERR ": Failure registering with the primary"
271                                "security module.\n");
272                         return -EINVAL;
273                 }
274                 secondary = 1;
275         }
276
277         /* initialize the uids vector */
278         memset(allowed_uids, 0, sizeof(allowed_uids));
279
280         lowmem_table_header = register_sysctl_table(lowmem_root_table);
281         if (unlikely(!lowmem_table_header))
282                 return -EPERM;
283
284         r = sysfs_create_group(kernel_kobj,
285                                &lowmem_attr_group);
286         if (unlikely(r))
287                 return r;
288
289         printk(KERN_INFO MY_NAME ": Module initialized.\n");
290
291         return 0;
292 }
293
294 static void __exit lowmem_exit(void)
295 {
296         /* remove ourselves from the security framework */
297         if (secondary) {
298                 if (mod_unreg_security(MY_NAME, &lowmem_security_ops))
299                         printk(KERN_ERR MY_NAME ": Failure unregistering "
300                                "with the primary security module.\n");
301         } else {
302                 if (unregister_security(&lowmem_security_ops)) {
303                         printk(KERN_ERR MY_NAME ": Failure unregistering "
304                                "with the kernel.\n");
305                 }
306         }
307
308         unregister_sysctl_table(lowmem_table_header);
309
310         sysfs_remove_group(kernel_kobj, &lowmem_attr_group);
311
312         printk(KERN_INFO MY_NAME ": Module removed.\n");
313 }
314
315 module_init(lowmem_init);
316 module_exit(lowmem_exit);
317
318 MODULE_DESCRIPTION("Low watermark LSM module");
319 MODULE_LICENSE("GPL");