4 * Sends low-memory notifications to processes via /dev/low-mem.
6 * Copyright (C) 2012 The Chromium OS Authors
7 * This program is free software, released under the GPL.
8 * Based on a proposal by Minchan Kim
10 * A process that polls /dev/low-mem is notified of a low-memory situation.
11 * The intent is to allow the process to free some memory before the OOM killer
14 * A low-memory condition is estimated by subtracting anonymous memory
15 * (i.e. process data segments), kernel memory, and a fixed amount of
16 * file-backed memory from total memory. This is just a heuristic, as in
17 * general we don't know how much memory can be reclaimed before we try to
18 * reclaim it, and that's too expensive or too late.
20 * This is tailored to Chromium OS, where a single program (the browser)
21 * controls most of the memory, and (currently) no swap space is used.
25 #include <linux/low-mem-notify.h>
26 #include <linux/module.h>
27 #include <linux/sched.h>
28 #include <linux/wait.h>
29 #include <linux/poll.h>
30 #include <linux/slab.h>
33 static DECLARE_WAIT_QUEUE_HEAD(low_mem_wait);
34 static atomic_t low_mem_state = ATOMIC_INIT(0);
35 unsigned low_mem_margin_percent = 10;
36 unsigned long low_mem_minfree;
38 struct low_mem_notify_file_info {
42 void low_mem_notify(void)
44 atomic_set(&low_mem_state, true);
45 wake_up(&low_mem_wait);
48 static int low_mem_notify_open(struct inode *inode, struct file *file)
50 struct low_mem_notify_file_info *info;
53 info = kmalloc(sizeof(*info), GFP_KERNEL);
59 file->private_data = info;
64 static int low_mem_notify_release(struct inode *inode, struct file *file)
66 kfree(file->private_data);
70 static unsigned int low_mem_notify_poll(struct file *file, poll_table *wait)
74 /* Update state to reflect any recent freeing. */
75 atomic_set(&low_mem_state, is_low_mem_situation());
77 poll_wait(file, &low_mem_wait, wait);
79 if (atomic_read(&low_mem_state) != 0)
85 const struct file_operations low_mem_notify_fops = {
86 .open = low_mem_notify_open,
87 .release = low_mem_notify_release,
88 .poll = low_mem_notify_poll,
90 EXPORT_SYMBOL(low_mem_notify_fops);
94 #define LOW_MEM_ATTR(_name) \
95 static struct kobj_attribute low_mem_##_name##_attr = \
96 __ATTR(_name, 0644, low_mem_##_name##_show, \
97 low_mem_##_name##_store)
99 static ssize_t low_mem_margin_show(struct kobject *kobj,
100 struct kobj_attribute *attr, char *buf)
102 return sprintf(buf, "%u\n", low_mem_margin_percent);
105 static unsigned low_mem_margin_to_minfree(unsigned percent)
107 return percent * totalram_pages / 100;
110 static ssize_t low_mem_margin_store(struct kobject *kobj,
111 struct kobj_attribute *attr,
112 const char *buf, size_t count)
115 unsigned long margin;
117 err = strict_strtoul(buf, 10, &margin);
118 if (err || margin > 99)
120 /* Notify when the percentage of "free" memory is below margin. */
121 low_mem_margin_percent = (unsigned int) margin;
122 /* Precompute as much as possible outside the allocator fast path. */
123 low_mem_minfree = low_mem_margin_to_minfree(low_mem_margin_percent);
124 printk(KERN_INFO "low_mem: setting minfree to %lu\n", low_mem_minfree);
127 LOW_MEM_ATTR(margin);
129 static struct attribute *low_mem_attrs[] = {
130 &low_mem_margin_attr.attr,
134 static struct attribute_group low_mem_attr_group = {
135 .attrs = low_mem_attrs,
139 static int __init low_mem_init(void)
141 int err = sysfs_create_group(mm_kobj, &low_mem_attr_group);
143 printk(KERN_ERR "low_mem: register sysfs failed\n");
144 low_mem_minfree = low_mem_margin_to_minfree(low_mem_margin_percent);
147 module_init(low_mem_init)