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