cregit-Linux how code gets into the kernel

Release 4.14 arch/s390/mm/cmm.c

Directory: arch/s390/mm
/*
 *  Collaborative memory management interface.
 *
 *    Copyright IBM Corp 2003, 2010
 *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
 *
 */

#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/gfp.h>
#include <linux/sched.h>
#include <linux/sysctl.h>
#include <linux/ctype.h>
#include <linux/swap.h>
#include <linux/kthread.h>
#include <linux/oom.h>
#include <linux/suspend.h>
#include <linux/uaccess.h>

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

#ifdef CONFIG_CMM_IUCV

static char *cmm_default_sender = "VMRMSVM";
#endif

static char *sender;
module_param(sender, charp, 0400);
MODULE_PARM_DESC(sender,
		 "Guest name that may send SMSG messages (default VMRMSVM)");

#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];
};


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;

static int cmm_suspended;


static struct cmm_page_array *cmm_page_list;

static struct cmm_page_array *cmm_timed_page_list;
static DEFINE_SPINLOCK(cmm_lock);


static struct task_struct *cmm_thread_ptr;
static DECLARE_WAIT_QUEUE_HEAD(cmm_thread_wait);
static DEFINE_TIMER(cmm_timer, NULL, 0, 0);

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


static long cmm_alloc_pages(long nr, long *counter, struct cmm_page_array **list) { struct cmm_page_array *pa, *npa; unsigned long addr; while (nr) { addr = __get_free_page(GFP_NOIO); if (!addr) break; spin_lock(&cmm_lock); pa = *list; if (!pa || pa->index >= CMM_NR_PAGES) { /* Need a new page for the page list. */ spin_unlock(&cmm_lock); npa = (struct cmm_page_array *) __get_free_page(GFP_NOIO); if (!npa) { free_page(addr); break; } 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); } diag10_range(addr >> PAGE_SHIFT, 1); pa->pages[pa->index++] = addr; (*counter)++; spin_unlock(&cmm_lock); nr--; } return nr; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton11859.00%133.33%
Martin Schwidefsky7738.50%133.33%
Michael Holzheu52.50%133.33%
Total200100.00%3100.00%


static long cmm_free_pages(long nr, long *counter, struct cmm_page_array **list) { struct cmm_page_array *pa; unsigned long addr; spin_lock(&cmm_lock); pa = *list; while (nr) { if (!pa || pa->index <= 0) break; addr = pa->pages[--pa->index]; if (pa->index == 0) { pa = pa->next; free_page((unsigned long) *list); *list = pa; } free_page(addr); (*counter)--; nr--; } spin_unlock(&cmm_lock); return nr; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton10182.11%150.00%
Martin Schwidefsky2217.89%150.00%
Total123100.00%2100.00%


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; }

Contributors

PersonTokensPropCommitsCommitProp
Martin Schwidefsky81100.00%1100.00%
Total81100.00%1100.00%

static struct notifier_block cmm_oom_nb = { .notifier_call = cmm_oom_notify, };
static int cmm_thread(void *dummy) { int rc; while (1) { rc = wait_event_interruptible(cmm_thread_wait, (!cmm_suspended && (cmm_pages != cmm_pages_target || cmm_timed_pages != cmm_timed_pages_target)) || kthread_should_stop()); if (kthread_should_stop() || rc == -ERESTARTSYS) { 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; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton16293.10%133.33%
Martin Schwidefsky63.45%133.33%
Heiko Carstens63.45%133.33%
Total174100.00%3100.00%


static void cmm_kick_thread(void) { wake_up(&cmm_thread_wait); }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton14100.00%1100.00%
Total14100.00%1100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton88100.00%1100.00%
Total88100.00%1100.00%


static void cmm_timer_fn(unsigned long ignored) { long nr; nr = cmm_timed_pages_target - cmm_timeout_pages; if (nr < 0) cmm_timed_pages_target = 0; else cmm_timed_pages_target = nr; cmm_kick_thread(); cmm_set_timer(); }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton3690.00%150.00%
Martin Schwidefsky410.00%150.00%
Total40100.00%2100.00%


static void cmm_set_pages(long nr) { cmm_pages_target = nr; cmm_kick_thread(); }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton1275.00%133.33%
Heiko Carstens212.50%133.33%
Martin Schwidefsky212.50%133.33%
Total16100.00%3100.00%


static long cmm_get_pages(void) { return cmm_pages; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton981.82%150.00%
Heiko Carstens218.18%150.00%
Total11100.00%2100.00%


static void cmm_add_timed_pages(long nr) { cmm_timed_pages_target += nr; cmm_kick_thread(); }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton1275.00%133.33%
Martin Schwidefsky212.50%133.33%
Heiko Carstens212.50%133.33%
Total16100.00%3100.00%


static long cmm_get_timed_pages(void) { return cmm_timed_pages; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton981.82%150.00%
Heiko Carstens218.18%150.00%
Total11100.00%2100.00%


static void cmm_set_timeout(long nr, long seconds) { cmm_timeout_pages = nr; cmm_timeout_seconds = seconds; cmm_set_timer(); }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton1982.61%133.33%
Heiko Carstens28.70%133.33%
Martin Schwidefsky28.70%133.33%
Total23100.00%3100.00%


static int cmm_skip_blanks(char *cp, char **endp) { char *str; for (str = cp; *str == ' ' || *str == '\t'; str++) ; *endp = str; return str != cp; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton49100.00%1100.00%
Total49100.00%1100.00%

static struct ctl_table cmm_table[];
static int cmm_pages_handler(struct ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos) { char buf[16], *p; unsigned int len; long nr; 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); nr = simple_strtoul(p, &p, 0); if (ctl == &cmm_table[0]) cmm_set_pages(nr); else cmm_add_timed_pages(nr); } else { if (ctl == &cmm_table[0]) nr = cmm_get_pages(); else nr = cmm_get_timed_pages(); len = sprintf(buf, "%ld\n", nr); if (len > *lenp) len = *lenp; if (copy_to_user(buffer, buf, len)) return -EFAULT; } *lenp = len; *ppos += len; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton21090.13%114.29%
Linus Torvalds83.43%114.29%
Heiko Carstens73.00%228.57%
Martin Schwidefsky62.58%114.29%
Joe Perches10.43%114.29%
Al Viro10.43%114.29%
Total233100.00%7100.00%


static int cmm_timeout_handler(struct ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos) { char buf[64], *p; long nr, seconds; unsigned 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); nr = simple_strtoul(p, &p, 0); cmm_skip_blanks(p, &p); seconds = simple_strtoul(p, &p, 0); cmm_set_timeout(nr, seconds); } 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; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton20290.99%114.29%
Linus Torvalds83.60%114.29%
Heiko Carstens73.15%228.57%
Martin Schwidefsky31.35%114.29%
Al Viro10.45%114.29%
Joe Perches10.45%114.29%
Total222100.00%7100.00%

static struct ctl_table cmm_table[] = { { .procname = "cmm_pages", .mode = 0644, .proc_handler = cmm_pages_handler, }, { .procname = "cmm_timed_pages", .mode = 0644, .proc_handler = cmm_pages_handler, }, { .procname = "cmm_timeout", .mode = 0644, .proc_handler = cmm_timeout_handler, }, { } }; static struct ctl_table cmm_dir_table[] = { { .procname = "vm", .maxlen = 0, .mode = 0555, .child = cmm_table, }, { } }; #ifdef CONFIG_CMM_IUCV #define SMSG_PREFIX "CMM"
static void cmm_smsg_target(const char *from, char *msg) { long nr, seconds; if (strlen(sender) > 0 && strcmp(from, sender) != 0) return; if (!cmm_skip_blanks(msg + strlen(SMSG_PREFIX), &msg)) return; if (strncmp(msg, "SHRINK", 6) == 0) { if (!cmm_skip_blanks(msg + 6, &msg)) return; nr = simple_strtoul(msg, &msg, 0); cmm_skip_blanks(msg, &msg); if (*msg == '\0') cmm_set_pages(nr); } else if (strncmp(msg, "RELEASE", 7) == 0) { if (!cmm_skip_blanks(msg + 7, &msg)) return; nr = simple_strtoul(msg, &msg, 0); cmm_skip_blanks(msg, &msg); if (*msg == '\0') cmm_add_timed_pages(nr); } else if (strncmp(msg, "REUSE", 5) == 0) { if (!cmm_skip_blanks(msg + 5, &msg)) return; nr = simple_strtoul(msg, &msg, 0); if (!cmm_skip_blanks(msg, &msg)) return; seconds = simple_strtoul(msg, &msg, 0); cmm_skip_blanks(msg, &msg); if (*msg == '\0') cmm_set_timeout(nr, seconds); } }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton22483.90%120.00%
Martin Schwidefsky3011.24%240.00%
Heiko Carstens124.49%120.00%
Hendrik Brueckner10.37%120.00%
Total267100.00%5100.00%

#endif static struct ctl_table_header *cmm_sysctl_header;
static int cmm_suspend(void) { cmm_suspended = 1; cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list); cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Martin Schwidefsky37100.00%1100.00%
Total37100.00%1100.00%


static int cmm_resume(void) { cmm_suspended = 0; cmm_kick_thread(); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Martin Schwidefsky18100.00%1100.00%
Total18100.00%1100.00%


static int cmm_power_event(struct notifier_block *this, unsigned long event, void *ptr) { switch (event) { case PM_POST_HIBERNATION: return cmm_resume(); case PM_HIBERNATION_PREPARE: return cmm_suspend(); default: return NOTIFY_DONE; } }

Contributors

PersonTokensPropCommitsCommitProp
Martin Schwidefsky43100.00%1100.00%
Total43100.00%1100.00%

static struct notifier_block cmm_power_notifier = { .notifier_call = cmm_power_event, };
static int __init cmm_init(void) { int rc = -ENOMEM; cmm_sysctl_header = register_sysctl_table(cmm_dir_table); if (!cmm_sysctl_header) goto out_sysctl; #ifdef CONFIG_CMM_IUCV /* convert sender to uppercase characters */ if (sender) { int len = strlen(sender); while (len--) sender[len] = toupper(sender[len]); } else { sender = cmm_default_sender; } rc = smsg_register_callback(SMSG_PREFIX, cmm_smsg_target); if (rc < 0) goto out_smsg; #endif rc = register_oom_notifier(&cmm_oom_nb); if (rc < 0) goto out_oom_notify; rc = register_pm_notifier(&cmm_power_notifier); if (rc) goto out_pm; cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread"); if (!IS_ERR(cmm_thread_ptr)) return 0; rc = PTR_ERR(cmm_thread_ptr); unregister_pm_notifier(&cmm_power_notifier); out_pm: unregister_oom_notifier(&cmm_oom_nb); out_oom_notify: #ifdef CONFIG_CMM_IUCV smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target); out_smsg: #endif unregister_sysctl_table(cmm_sysctl_header); out_sysctl: del_timer_sync(&cmm_timer); return rc; }

Contributors

PersonTokensPropCommitsCommitProp
Heiko Carstens8643.00%440.00%
Andrew Morton3919.50%110.00%
Hendrik Brueckner3316.50%110.00%
Martin Schwidefsky3216.00%220.00%
Alexandru Gheorghiu94.50%110.00%
Al Viro10.50%110.00%
Total200100.00%10100.00%

module_init(cmm_init);
static void __exit cmm_exit(void) { unregister_sysctl_table(cmm_sysctl_header); #ifdef CONFIG_CMM_IUCV smsg_unregister_callback(SMSG_PREFIX, cmm_smsg_target); #endif unregister_pm_notifier(&cmm_power_notifier); unregister_oom_notifier(&cmm_oom_nb); kthread_stop(cmm_thread_ptr); del_timer_sync(&cmm_timer); cmm_free_pages(cmm_pages, &cmm_pages, &cmm_page_list); cmm_free_pages(cmm_timed_pages, &cmm_timed_pages, &cmm_timed_page_list); }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton3042.25%116.67%
Heiko Carstens2940.85%350.00%
Martin Schwidefsky1216.90%233.33%
Total71100.00%6100.00%

module_exit(cmm_exit); MODULE_LICENSE("GPL");

Overall Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton156568.43%13.70%
Martin Schwidefsky44019.24%414.81%
Heiko Carstens1998.70%1037.04%
Hendrik Brueckner341.49%27.41%
Linus Torvalds160.70%13.70%
Alexandru Gheorghiu90.39%13.70%
Michael Holzheu80.35%27.41%
David Rientjes30.13%13.70%
Al Viro30.13%13.70%
Tejun Heo30.13%13.70%
Paul Gortmaker30.13%13.70%
Joe Perches20.09%13.70%
Eric W. Biedermann20.09%13.70%
Total2287100.00%27100.00%
Directory: arch/s390/mm
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.