xref: /linux/arch/s390/mm/cmm.c (revision e190bfe56841551b1ad5abb42ebd0c4798cc8c01)
1 /*
2  *  Collaborative memory management interface.
3  *
4  *    Copyright IBM Corp 2003,2010
5  *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
6  *
7  */
8 
9 #include <linux/errno.h>
10 #include <linux/fs.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/gfp.h>
14 #include <linux/sched.h>
15 #include <linux/sysctl.h>
16 #include <linux/ctype.h>
17 #include <linux/swap.h>
18 #include <linux/kthread.h>
19 #include <linux/oom.h>
20 #include <linux/suspend.h>
21 #include <linux/uaccess.h>
22 
23 #include <asm/pgalloc.h>
24 #include <asm/diag.h>
25 
26 static char *sender = "VMRMSVM";
27 module_param(sender, charp, 0400);
28 MODULE_PARM_DESC(sender,
29 		 "Guest name that may send SMSG messages (default VMRMSVM)");
30 
31 #include "../../../drivers/s390/net/smsgiucv.h"
32 
33 #define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2)
34 
35 struct cmm_page_array {
36 	struct cmm_page_array *next;
37 	unsigned long index;
38 	unsigned long pages[CMM_NR_PAGES];
39 };
40 
41 static long cmm_pages;
42 static long cmm_timed_pages;
43 static volatile long cmm_pages_target;
44 static volatile long cmm_timed_pages_target;
45 static long cmm_timeout_pages;
46 static long cmm_timeout_seconds;
47 static int cmm_suspended;
48 
49 static struct cmm_page_array *cmm_page_list;
50 static struct cmm_page_array *cmm_timed_page_list;
51 static DEFINE_SPINLOCK(cmm_lock);
52 
53 static struct task_struct *cmm_thread_ptr;
54 static DECLARE_WAIT_QUEUE_HEAD(cmm_thread_wait);
55 static DEFINE_TIMER(cmm_timer, NULL, 0, 0);
56 
57 static void cmm_timer_fn(unsigned long);
58 static void cmm_set_timer(void);
59 
60 static long cmm_alloc_pages(long nr, long *counter,
61 			    struct cmm_page_array **list)
62 {
63 	struct cmm_page_array *pa, *npa;
64 	unsigned long addr;
65 
66 	while (nr) {
67 		addr = __get_free_page(GFP_NOIO);
68 		if (!addr)
69 			break;
70 		spin_lock(&cmm_lock);
71 		pa = *list;
72 		if (!pa || pa->index >= CMM_NR_PAGES) {
73 			/* Need a new page for the page list. */
74 			spin_unlock(&cmm_lock);
75 			npa = (struct cmm_page_array *)
76 				__get_free_page(GFP_NOIO);
77 			if (!npa) {
78 				free_page(addr);
79 				break;
80 			}
81 			spin_lock(&cmm_lock);
82 			pa = *list;
83 			if (!pa || pa->index >= CMM_NR_PAGES) {
84 				npa->next = pa;
85 				npa->index = 0;
86 				pa = npa;
87 				*list = pa;
88 			} else
89 				free_page((unsigned long) npa);
90 		}
91 		diag10(addr);
92 		pa->pages[pa->index++] = addr;
93 		(*counter)++;
94 		spin_unlock(&cmm_lock);
95 		nr--;
96 	}
97 	return nr;
98 }
99 
100 static long cmm_free_pages(long nr, long *counter, struct cmm_page_array **list)
101 {
102 	struct cmm_page_array *pa;
103 	unsigned long addr;
104 
105 	spin_lock(&cmm_lock);
106 	pa = *list;
107 	while (nr) {
108 		if (!pa || pa->index <= 0)
109 			break;
110 		addr = pa->pages[--pa->index];
111 		if (pa->index == 0) {
112 			pa = pa->next;
113 			free_page((unsigned long) *list);
114 			*list = pa;
115 		}
116 		free_page(addr);
117 		(*counter)--;
118 		nr--;
119 	}
120 	spin_unlock(&cmm_lock);
121 	return nr;
122 }
123 
124 static int cmm_oom_notify(struct notifier_block *self,
125 			  unsigned long dummy, void *parm)
126 {
127 	unsigned long *freed = parm;
128 	long nr = 256;
129 
130 	nr = cmm_free_pages(nr, &cmm_timed_pages, &cmm_timed_page_list);
131 	if (nr > 0)
132 		nr = cmm_free_pages(nr, &cmm_pages, &cmm_page_list);
133 	cmm_pages_target = cmm_pages;
134 	cmm_timed_pages_target = cmm_timed_pages;
135 	*freed += 256 - nr;
136 	return NOTIFY_OK;
137 }
138 
139 static struct notifier_block cmm_oom_nb = {
140 	.notifier_call = cmm_oom_notify,
141 };
142 
143 static int cmm_thread(void *dummy)
144 {
145 	int rc;
146 
147 	while (1) {
148 		rc = wait_event_interruptible(cmm_thread_wait,
149 			(!cmm_suspended && (cmm_pages != cmm_pages_target ||
150 			 cmm_timed_pages != cmm_timed_pages_target)) ||
151 			 kthread_should_stop());
152 		if (kthread_should_stop() || rc == -ERESTARTSYS) {
153 			cmm_pages_target = cmm_pages;
154 			cmm_timed_pages_target = cmm_timed_pages;
155 			break;
156 		}
157 		if (cmm_pages_target > cmm_pages) {
158 			if (cmm_alloc_pages(1, &cmm_pages, &cmm_page_list))
159 				cmm_pages_target = cmm_pages;
160 		} else if (cmm_pages_target < cmm_pages) {
161 			cmm_free_pages(1, &cmm_pages, &cmm_page_list);
162 		}
163 		if (cmm_timed_pages_target > cmm_timed_pages) {
164 			if (cmm_alloc_pages(1, &cmm_timed_pages,
165 					   &cmm_timed_page_list))
166 				cmm_timed_pages_target = cmm_timed_pages;
167 		} else if (cmm_timed_pages_target < cmm_timed_pages) {
168 			cmm_free_pages(1, &cmm_timed_pages,
169 				       &cmm_timed_page_list);
170 		}
171 		if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer))
172 			cmm_set_timer();
173 	}
174 	return 0;
175 }
176 
177 static void cmm_kick_thread(void)
178 {
179 	wake_up(&cmm_thread_wait);
180 }
181 
182 static void cmm_set_timer(void)
183 {
184 	if (cmm_timed_pages_target <= 0 || cmm_timeout_seconds <= 0) {
185 		if (timer_pending(&cmm_timer))
186 			del_timer(&cmm_timer);
187 		return;
188 	}
189 	if (timer_pending(&cmm_timer)) {
190 		if (mod_timer(&cmm_timer, jiffies + cmm_timeout_seconds*HZ))
191 			return;
192 	}
193 	cmm_timer.function = cmm_timer_fn;
194 	cmm_timer.data = 0;
195 	cmm_timer.expires = jiffies + cmm_timeout_seconds*HZ;
196 	add_timer(&cmm_timer);
197 }
198 
199 static void cmm_timer_fn(unsigned long ignored)
200 {
201 	long nr;
202 
203 	nr = cmm_timed_pages_target - cmm_timeout_pages;
204 	if (nr < 0)
205 		cmm_timed_pages_target = 0;
206 	else
207 		cmm_timed_pages_target = nr;
208 	cmm_kick_thread();
209 	cmm_set_timer();
210 }
211 
212 static void cmm_set_pages(long nr)
213 {
214 	cmm_pages_target = nr;
215 	cmm_kick_thread();
216 }
217 
218 static long cmm_get_pages(void)
219 {
220 	return cmm_pages;
221 }
222 
223 static void cmm_add_timed_pages(long nr)
224 {
225 	cmm_timed_pages_target += nr;
226 	cmm_kick_thread();
227 }
228 
229 static long cmm_get_timed_pages(void)
230 {
231 	return cmm_timed_pages;
232 }
233 
234 static void cmm_set_timeout(long nr, long seconds)
235 {
236 	cmm_timeout_pages = nr;
237 	cmm_timeout_seconds = seconds;
238 	cmm_set_timer();
239 }
240 
241 static int cmm_skip_blanks(char *cp, char **endp)
242 {
243 	char *str;
244 
245 	for (str = cp; *str == ' ' || *str == '\t'; str++)
246 		;
247 	*endp = str;
248 	return str != cp;
249 }
250 
251 static struct ctl_table cmm_table[];
252 
253 static int cmm_pages_handler(ctl_table *ctl, int write, void __user *buffer,
254 			     size_t *lenp, loff_t *ppos)
255 {
256 	char buf[16], *p;
257 	long nr;
258 	int len;
259 
260 	if (!*lenp || (*ppos && !write)) {
261 		*lenp = 0;
262 		return 0;
263 	}
264 
265 	if (write) {
266 		len = *lenp;
267 		if (copy_from_user(buf, buffer,
268 				   len > sizeof(buf) ? sizeof(buf) : len))
269 			return -EFAULT;
270 		buf[sizeof(buf) - 1] = '\0';
271 		cmm_skip_blanks(buf, &p);
272 		nr = simple_strtoul(p, &p, 0);
273 		if (ctl == &cmm_table[0])
274 			cmm_set_pages(nr);
275 		else
276 			cmm_add_timed_pages(nr);
277 	} else {
278 		if (ctl == &cmm_table[0])
279 			nr = cmm_get_pages();
280 		else
281 			nr = cmm_get_timed_pages();
282 		len = sprintf(buf, "%ld\n", nr);
283 		if (len > *lenp)
284 			len = *lenp;
285 		if (copy_to_user(buffer, buf, len))
286 			return -EFAULT;
287 	}
288 	*lenp = len;
289 	*ppos += len;
290 	return 0;
291 }
292 
293 static int cmm_timeout_handler(ctl_table *ctl, int write,  void __user *buffer,
294 			       size_t *lenp, loff_t *ppos)
295 {
296 	char buf[64], *p;
297 	long nr, seconds;
298 	int len;
299 
300 	if (!*lenp || (*ppos && !write)) {
301 		*lenp = 0;
302 		return 0;
303 	}
304 
305 	if (write) {
306 		len = *lenp;
307 		if (copy_from_user(buf, buffer,
308 				   len > sizeof(buf) ? sizeof(buf) : len))
309 			return -EFAULT;
310 		buf[sizeof(buf) - 1] = '\0';
311 		cmm_skip_blanks(buf, &p);
312 		nr = simple_strtoul(p, &p, 0);
313 		cmm_skip_blanks(p, &p);
314 		seconds = simple_strtoul(p, &p, 0);
315 		cmm_set_timeout(nr, seconds);
316 	} else {
317 		len = sprintf(buf, "%ld %ld\n",
318 			      cmm_timeout_pages, cmm_timeout_seconds);
319 		if (len > *lenp)
320 			len = *lenp;
321 		if (copy_to_user(buffer, buf, len))
322 			return -EFAULT;
323 	}
324 	*lenp = len;
325 	*ppos += len;
326 	return 0;
327 }
328 
329 static struct ctl_table cmm_table[] = {
330 	{
331 		.procname	= "cmm_pages",
332 		.mode		= 0644,
333 		.proc_handler	= cmm_pages_handler,
334 	},
335 	{
336 		.procname	= "cmm_timed_pages",
337 		.mode		= 0644,
338 		.proc_handler	= cmm_pages_handler,
339 	},
340 	{
341 		.procname	= "cmm_timeout",
342 		.mode		= 0644,
343 		.proc_handler	= cmm_timeout_handler,
344 	},
345 	{ }
346 };
347 
348 static struct ctl_table cmm_dir_table[] = {
349 	{
350 		.procname	= "vm",
351 		.maxlen		= 0,
352 		.mode		= 0555,
353 		.child		= cmm_table,
354 	},
355 	{ }
356 };
357 
358 #ifdef CONFIG_CMM_IUCV
359 #define SMSG_PREFIX "CMM"
360 static void cmm_smsg_target(const char *from, char *msg)
361 {
362 	long nr, seconds;
363 
364 	if (strlen(sender) > 0 && strcmp(from, sender) != 0)
365 		return;
366 	if (!cmm_skip_blanks(msg + strlen(SMSG_PREFIX), &msg))
367 		return;
368 	if (strncmp(msg, "SHRINK", 6) == 0) {
369 		if (!cmm_skip_blanks(msg + 6, &msg))
370 			return;
371 		nr = simple_strtoul(msg, &msg, 0);
372 		cmm_skip_blanks(msg, &msg);
373 		if (*msg == '\0')
374 			cmm_set_pages(nr);
375 	} else if (strncmp(msg, "RELEASE", 7) == 0) {
376 		if (!cmm_skip_blanks(msg + 7, &msg))
377 			return;
378 		nr = simple_strtoul(msg, &msg, 0);
379 		cmm_skip_blanks(msg, &msg);
380 		if (*msg == '\0')
381 			cmm_add_timed_pages(nr);
382 	} else if (strncmp(msg, "REUSE", 5) == 0) {
383 		if (!cmm_skip_blanks(msg + 5, &msg))
384 			return;
385 		nr = simple_strtoul(msg, &msg, 0);
386 		if (!cmm_skip_blanks(msg, &msg))
387 			return;
388 		seconds = simple_strtoul(msg, &msg, 0);
389 		cmm_skip_blanks(msg, &msg);
390 		if (*msg == '\0')
391 			cmm_set_timeout(nr, seconds);
392 	}
393 }
394 #endif
395 
396 static struct ctl_table_header *cmm_sysctl_header;
397 
398 static int cmm_suspend(void)
399 {
400 	cmm_suspended = 1;
401 	cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list);
402 	cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list);
403 	return 0;
404 }
405 
406 static int cmm_resume(void)
407 {
408 	cmm_suspended = 0;
409 	cmm_kick_thread();
410 	return 0;
411 }
412 
413 static int cmm_power_event(struct notifier_block *this,
414 			   unsigned long event, void *ptr)
415 {
416 	switch (event) {
417 	case PM_POST_HIBERNATION:
418 		return cmm_resume();
419 	case PM_HIBERNATION_PREPARE:
420 		return cmm_suspend();
421 	default:
422 		return NOTIFY_DONE;
423 	}
424 }
425 
426 static struct notifier_block cmm_power_notifier = {
427 	.notifier_call = cmm_power_event,
428 };
429 
430 static int cmm_init(void)
431 {
432 	int rc = -ENOMEM;
433 
434 	cmm_sysctl_header = register_sysctl_table(cmm_dir_table);
435 	if (!cmm_sysctl_header)
436 		goto out_sysctl;
437 #ifdef CONFIG_CMM_IUCV
438 	rc = smsg_register_callback(SMSG_PREFIX, cmm_smsg_target);
439 	if (rc < 0)
440 		goto out_smsg;
441 #endif
442 	rc = register_oom_notifier(&cmm_oom_nb);
443 	if (rc < 0)
444 		goto out_oom_notify;
445 	rc = register_pm_notifier(&cmm_power_notifier);
446 	if (rc)
447 		goto out_pm;
448 	cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
449 	rc = IS_ERR(cmm_thread_ptr) ? PTR_ERR(cmm_thread_ptr) : 0;
450 	if (rc)
451 		goto out_kthread;
452 	return 0;
453 
454 out_kthread:
455 	unregister_pm_notifier(&cmm_power_notifier);
456 out_pm:
457 	unregister_oom_notifier(&cmm_oom_nb);
458 out_oom_notify:
459 #ifdef CONFIG_CMM_IUCV
460 	smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
461 out_smsg:
462 #endif
463 	unregister_sysctl_table(cmm_sysctl_header);
464 out_sysctl:
465 	del_timer_sync(&cmm_timer);
466 	return rc;
467 }
468 module_init(cmm_init);
469 
470 static void cmm_exit(void)
471 {
472 	unregister_sysctl_table(cmm_sysctl_header);
473 #ifdef CONFIG_CMM_IUCV
474 	smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
475 #endif
476 	unregister_pm_notifier(&cmm_power_notifier);
477 	unregister_oom_notifier(&cmm_oom_nb);
478 	kthread_stop(cmm_thread_ptr);
479 	del_timer_sync(&cmm_timer);
480 	cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list);
481 	cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list);
482 }
483 module_exit(cmm_exit);
484 
485 MODULE_LICENSE("GPL");
486