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