cmm.c 9.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *  arch/s390/mm/cmm.c
 *
 *  S390 version
 *    Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
 *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
 *
 *  Collaborative memory management interface.
 */

#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/sysctl.h>
#include <linux/ctype.h>
18
#include <linux/swap.h>
L
Linus Torvalds 已提交
19 20 21 22

#include <asm/pgalloc.h>
#include <asm/uaccess.h>

23
static char *sender = "VMRMSVM";
24
module_param(sender, charp, 0400);
25 26 27
MODULE_PARM_DESC(sender,
		 "Guest name that may send SMSG messages (default VMRMSVM)");

L
Linus Torvalds 已提交
28 29 30 31 32 33 34 35 36 37
#include "../../../drivers/s390/net/smsgiucv.h"

#define CMM_NR_PAGES ((PAGE_SIZE / sizeof(unsigned long)) - 2)

struct cmm_page_array {
	struct cmm_page_array *next;
	unsigned long index;
	unsigned long pages[CMM_NR_PAGES];
};

38 39 40 41 42 43
static long cmm_pages;
static long cmm_timed_pages;
static volatile long cmm_pages_target;
static volatile long cmm_timed_pages_target;
static long cmm_timeout_pages;
static long cmm_timeout_seconds;
L
Linus Torvalds 已提交
44

45 46 47
static struct cmm_page_array *cmm_page_list;
static struct cmm_page_array *cmm_timed_page_list;
static DEFINE_SPINLOCK(cmm_lock);
L
Linus Torvalds 已提交
48

49
static unsigned long cmm_thread_active;
L
Linus Torvalds 已提交
50 51 52 53 54 55 56 57
static struct work_struct cmm_thread_starter;
static wait_queue_head_t cmm_thread_wait;
static struct timer_list cmm_timer;

static void cmm_timer_fn(unsigned long);
static void cmm_set_timer(void);

static long
58
cmm_alloc_pages(long nr, long *counter, struct cmm_page_array **list)
L
Linus Torvalds 已提交
59
{
60 61
	struct cmm_page_array *pa, *npa;
	unsigned long addr;
L
Linus Torvalds 已提交
62

63 64 65
	while (nr) {
		addr = __get_free_page(GFP_NOIO);
		if (!addr)
L
Linus Torvalds 已提交
66
			break;
67 68
		spin_lock(&cmm_lock);
		pa = *list;
L
Linus Torvalds 已提交
69 70
		if (!pa || pa->index >= CMM_NR_PAGES) {
			/* Need a new page for the page list. */
71 72
			spin_unlock(&cmm_lock);
			npa = (struct cmm_page_array *)
L
Linus Torvalds 已提交
73
				__get_free_page(GFP_NOIO);
74 75
			if (!npa) {
				free_page(addr);
L
Linus Torvalds 已提交
76 77
				break;
			}
78 79 80 81 82 83 84 85 86
			spin_lock(&cmm_lock);
			pa = *list;
			if (!pa || pa->index >= CMM_NR_PAGES) {
				npa->next = pa;
				npa->index = 0;
				pa = npa;
				*list = pa;
			} else
				free_page((unsigned long) npa);
L
Linus Torvalds 已提交
87
		}
88 89
		diag10(addr);
		pa->pages[pa->index++] = addr;
L
Linus Torvalds 已提交
90
		(*counter)++;
91 92
		spin_unlock(&cmm_lock);
		nr--;
L
Linus Torvalds 已提交
93
	}
94
	return nr;
L
Linus Torvalds 已提交
95 96
}

97 98
static long
cmm_free_pages(long nr, long *counter, struct cmm_page_array **list)
L
Linus Torvalds 已提交
99 100
{
	struct cmm_page_array *pa;
101
	unsigned long addr;
L
Linus Torvalds 已提交
102

103
	spin_lock(&cmm_lock);
L
Linus Torvalds 已提交
104
	pa = *list;
105
	while (nr) {
L
Linus Torvalds 已提交
106 107
		if (!pa || pa->index <= 0)
			break;
108
		addr = pa->pages[--pa->index];
L
Linus Torvalds 已提交
109 110 111 112 113
		if (pa->index == 0) {
			pa = pa->next;
			free_page((unsigned long) *list);
			*list = pa;
		}
114
		free_page(addr);
L
Linus Torvalds 已提交
115
		(*counter)--;
116
		nr--;
L
Linus Torvalds 已提交
117
	}
118 119
	spin_unlock(&cmm_lock);
	return nr;
L
Linus Torvalds 已提交
120 121
}

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
static int cmm_oom_notify(struct notifier_block *self,
			  unsigned long dummy, void *parm)
{
	unsigned long *freed = parm;
	long nr = 256;

	nr = cmm_free_pages(nr, &cmm_timed_pages, &cmm_timed_page_list);
	if (nr > 0)
		nr = cmm_free_pages(nr, &cmm_pages, &cmm_page_list);
	cmm_pages_target = cmm_pages;
	cmm_timed_pages_target = cmm_timed_pages;
	*freed += 256 - nr;
	return NOTIFY_OK;
}

static struct notifier_block cmm_oom_nb = {
	.notifier_call = cmm_oom_notify
};

L
Linus Torvalds 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
static int
cmm_thread(void *dummy)
{
	int rc;

	daemonize("cmmthread");
	while (1) {
		rc = wait_event_interruptible(cmm_thread_wait,
			(cmm_pages != cmm_pages_target ||
			 cmm_timed_pages != cmm_timed_pages_target));
		if (rc == -ERESTARTSYS) {
			/* Got kill signal. End thread. */
			clear_bit(0, &cmm_thread_active);
			cmm_pages_target = cmm_pages;
			cmm_timed_pages_target = cmm_timed_pages;
			break;
		}
		if (cmm_pages_target > cmm_pages) {
			if (cmm_alloc_pages(1, &cmm_pages, &cmm_page_list))
				cmm_pages_target = cmm_pages;
		} else if (cmm_pages_target < cmm_pages) {
			cmm_free_pages(1, &cmm_pages, &cmm_page_list);
		}
		if (cmm_timed_pages_target > cmm_timed_pages) {
			if (cmm_alloc_pages(1, &cmm_timed_pages,
					   &cmm_timed_page_list))
				cmm_timed_pages_target = cmm_timed_pages;
		} else if (cmm_timed_pages_target < cmm_timed_pages) {
			cmm_free_pages(1, &cmm_timed_pages,
			       	       &cmm_timed_page_list);
		}
		if (cmm_timed_pages > 0 && !timer_pending(&cmm_timer))
			cmm_set_timer();
	}
	return 0;
}

static void
cmm_start_thread(void)
{
H
Heiko Carstens 已提交
181
	kernel_thread(cmm_thread, NULL, 0);
L
Linus Torvalds 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
}

static void
cmm_kick_thread(void)
{
	if (!test_and_set_bit(0, &cmm_thread_active))
		schedule_work(&cmm_thread_starter);
	wake_up(&cmm_thread_wait);
}

static void
cmm_set_timer(void)
{
	if (cmm_timed_pages_target <= 0 || cmm_timeout_seconds <= 0) {
		if (timer_pending(&cmm_timer))
			del_timer(&cmm_timer);
		return;
	}
	if (timer_pending(&cmm_timer)) {
		if (mod_timer(&cmm_timer, jiffies + cmm_timeout_seconds*HZ))
			return;
	}
	cmm_timer.function = cmm_timer_fn;
	cmm_timer.data = 0;
	cmm_timer.expires = jiffies + cmm_timeout_seconds*HZ;
	add_timer(&cmm_timer);
}

static void
cmm_timer_fn(unsigned long ignored)
{
213
	long nr;
L
Linus Torvalds 已提交
214

215 216
	nr = cmm_timed_pages_target - cmm_timeout_pages;
	if (nr < 0)
L
Linus Torvalds 已提交
217 218
		cmm_timed_pages_target = 0;
	else
219
		cmm_timed_pages_target = nr;
L
Linus Torvalds 已提交
220 221 222 223 224
	cmm_kick_thread();
	cmm_set_timer();
}

void
225
cmm_set_pages(long nr)
L
Linus Torvalds 已提交
226
{
227
	cmm_pages_target = nr;
L
Linus Torvalds 已提交
228 229 230 231 232 233 234 235 236 237
	cmm_kick_thread();
}

long
cmm_get_pages(void)
{
	return cmm_pages;
}

void
238
cmm_add_timed_pages(long nr)
L
Linus Torvalds 已提交
239
{
240
	cmm_timed_pages_target += nr;
L
Linus Torvalds 已提交
241 242 243 244 245 246 247 248 249 250
	cmm_kick_thread();
}

long
cmm_get_timed_pages(void)
{
	return cmm_timed_pages;
}

void
251
cmm_set_timeout(long nr, long seconds)
L
Linus Torvalds 已提交
252
{
253
	cmm_timeout_pages = nr;
L
Linus Torvalds 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	cmm_timeout_seconds = seconds;
	cmm_set_timer();
}

static inline int
cmm_skip_blanks(char *cp, char **endp)
{
	char *str;

	for (str = cp; *str == ' ' || *str == '\t'; str++);
	*endp = str;
	return str != cp;
}

#ifdef CONFIG_CMM_PROC
/* These will someday get removed. */
#define VM_CMM_PAGES		1111
#define VM_CMM_TIMED_PAGES	1112
#define VM_CMM_TIMEOUT		1113

static struct ctl_table cmm_table[];

static int
cmm_pages_handler(ctl_table *ctl, int write, struct file *filp,
278
		  void __user *buffer, size_t *lenp, loff_t *ppos)
L
Linus Torvalds 已提交
279 280
{
	char buf[16], *p;
281
	long nr;
L
Linus Torvalds 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295
	int len;

	if (!*lenp || (*ppos && !write)) {
		*lenp = 0;
		return 0;
	}

	if (write) {
		len = *lenp;
		if (copy_from_user(buf, buffer,
				   len > sizeof(buf) ? sizeof(buf) : len))
			return -EFAULT;
		buf[sizeof(buf) - 1] = '\0';
		cmm_skip_blanks(buf, &p);
296
		nr = simple_strtoul(p, &p, 0);
L
Linus Torvalds 已提交
297
		if (ctl == &cmm_table[0])
298
			cmm_set_pages(nr);
L
Linus Torvalds 已提交
299
		else
300
			cmm_add_timed_pages(nr);
L
Linus Torvalds 已提交
301 302
	} else {
		if (ctl == &cmm_table[0])
303
			nr = cmm_get_pages();
L
Linus Torvalds 已提交
304
		else
305 306
			nr = cmm_get_timed_pages();
		len = sprintf(buf, "%ld\n", nr);
L
Linus Torvalds 已提交
307 308 309 310 311 312 313 314 315 316 317 318
		if (len > *lenp)
			len = *lenp;
		if (copy_to_user(buffer, buf, len))
			return -EFAULT;
	}
	*lenp = len;
	*ppos += len;
	return 0;
}

static int
cmm_timeout_handler(ctl_table *ctl, int write, struct file *filp,
319
		    void __user *buffer, size_t *lenp, loff_t *ppos)
L
Linus Torvalds 已提交
320 321
{
	char buf[64], *p;
322
	long nr, seconds;
L
Linus Torvalds 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336
	int len;

	if (!*lenp || (*ppos && !write)) {
		*lenp = 0;
		return 0;
	}

	if (write) {
		len = *lenp;
		if (copy_from_user(buf, buffer,
				   len > sizeof(buf) ? sizeof(buf) : len))
			return -EFAULT;
		buf[sizeof(buf) - 1] = '\0';
		cmm_skip_blanks(buf, &p);
337
		nr = simple_strtoul(p, &p, 0);
L
Linus Torvalds 已提交
338
		cmm_skip_blanks(p, &p);
339
		seconds = simple_strtoul(p, &p, 0);
340
		cmm_set_timeout(nr, seconds);
L
Linus Torvalds 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	} else {
		len = sprintf(buf, "%ld %ld\n",
			      cmm_timeout_pages, cmm_timeout_seconds);
		if (len > *lenp)
			len = *lenp;
		if (copy_to_user(buffer, buf, len))
			return -EFAULT;
	}
	*lenp = len;
	*ppos += len;
	return 0;
}

static struct ctl_table cmm_table[] = {
	{
		.ctl_name	= VM_CMM_PAGES,
		.procname	= "cmm_pages",
358
		.mode		= 0644,
L
Linus Torvalds 已提交
359 360 361 362 363
		.proc_handler	= &cmm_pages_handler,
	},
	{
		.ctl_name	= VM_CMM_TIMED_PAGES,
		.procname	= "cmm_timed_pages",
364
		.mode		= 0644,
L
Linus Torvalds 已提交
365 366 367 368 369
		.proc_handler	= &cmm_pages_handler,
	},
	{
		.ctl_name	= VM_CMM_TIMEOUT,
		.procname	= "cmm_timeout",
370
		.mode		= 0644,
L
Linus Torvalds 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
		.proc_handler	= &cmm_timeout_handler,
	},
	{ .ctl_name = 0 }
};

static struct ctl_table cmm_dir_table[] = {
	{
		.ctl_name	= CTL_VM,
		.procname	= "vm",
		.maxlen		= 0,
		.mode		= 0555,
		.child		= cmm_table,
	},
	{ .ctl_name = 0 }
};
#endif

#ifdef CONFIG_CMM_IUCV
#define SMSG_PREFIX "CMM"
static void
391
cmm_smsg_target(char *from, char *msg)
L
Linus Torvalds 已提交
392
{
393
	long nr, seconds;
L
Linus Torvalds 已提交
394

395 396
	if (strlen(sender) > 0 && strcmp(from, sender) != 0)
		return;
L
Linus Torvalds 已提交
397 398 399 400 401
	if (!cmm_skip_blanks(msg + strlen(SMSG_PREFIX), &msg))
		return;
	if (strncmp(msg, "SHRINK", 6) == 0) {
		if (!cmm_skip_blanks(msg + 6, &msg))
			return;
402
		nr = simple_strtoul(msg, &msg, 0);
L
Linus Torvalds 已提交
403 404
		cmm_skip_blanks(msg, &msg);
		if (*msg == '\0')
405
			cmm_set_pages(nr);
L
Linus Torvalds 已提交
406 407 408
	} else if (strncmp(msg, "RELEASE", 7) == 0) {
		if (!cmm_skip_blanks(msg + 7, &msg))
			return;
409
		nr = simple_strtoul(msg, &msg, 0);
L
Linus Torvalds 已提交
410 411
		cmm_skip_blanks(msg, &msg);
		if (*msg == '\0')
412
			cmm_add_timed_pages(nr);
L
Linus Torvalds 已提交
413 414 415
	} else if (strncmp(msg, "REUSE", 5) == 0) {
		if (!cmm_skip_blanks(msg + 5, &msg))
			return;
416
		nr = simple_strtoul(msg, &msg, 0);
L
Linus Torvalds 已提交
417 418
		if (!cmm_skip_blanks(msg, &msg))
			return;
419
		seconds = simple_strtoul(msg, &msg, 0);
L
Linus Torvalds 已提交
420 421
		cmm_skip_blanks(msg, &msg);
		if (*msg == '\0')
422
			cmm_set_timeout(nr, seconds);
L
Linus Torvalds 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
	}
}
#endif

struct ctl_table_header *cmm_sysctl_header;

static int
cmm_init (void)
{
#ifdef CONFIG_CMM_PROC
	cmm_sysctl_header = register_sysctl_table(cmm_dir_table, 1);
#endif
#ifdef CONFIG_CMM_IUCV
	smsg_register_callback(SMSG_PREFIX, cmm_smsg_target);
#endif
438
	register_oom_notifier(&cmm_oom_nb);
439
	INIT_WORK(&cmm_thread_starter, (void *) cmm_start_thread, NULL);
L
Linus Torvalds 已提交
440 441 442 443 444 445 446 447
	init_waitqueue_head(&cmm_thread_wait);
	init_timer(&cmm_timer);
	return 0;
}

static void
cmm_exit(void)
{
448
	unregister_oom_notifier(&cmm_oom_nb);
L
Linus Torvalds 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
	cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list);
	cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list);
#ifdef CONFIG_CMM_PROC
	unregister_sysctl_table(cmm_sysctl_header);
#endif
#ifdef CONFIG_CMM_IUCV
	smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target);
#endif
}

module_init(cmm_init);
module_exit(cmm_exit);

EXPORT_SYMBOL(cmm_set_pages);
EXPORT_SYMBOL(cmm_get_pages);
EXPORT_SYMBOL(cmm_add_timed_pages);
EXPORT_SYMBOL(cmm_get_timed_pages);
EXPORT_SYMBOL(cmm_set_timeout);

MODULE_LICENSE("GPL");