1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * mm/lmkd_dbg_trigger.c
4 *
5 * Copyright (c) 2020-2022 Huawei Technologies Co., Ltd.
6 */
7 #include <linux/atomic.h>
8 #include <linux/fs.h>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/oom.h>
13 #include <linux/proc_fs.h>
14 #include <linux/seq_file.h>
15 #include <linux/uaccess.h>
16 #include <linux/lowmem_dbg.h>
17 #define PROC_NUMBUF 8
18
19 static int lmkd_oom_score_adj;
20 static atomic64_t lmkd_no_cma_cnt = ATOMIC64_INIT(0);
21
lmkd_dbg_trigger_proc_show(struct seq_file * m,void * v)22 static int lmkd_dbg_trigger_proc_show(struct seq_file *m, void *v)
23 {
24 seq_printf(m, "lmkd_oom_score_adj: %d\n", lmkd_oom_score_adj);
25 seq_printf(m, "lmkd_no_cma_cnt: %lld\n",
26 atomic64_read(&lmkd_no_cma_cnt));
27 return 0;
28 }
29
lmkd_dbg_trigger_proc_open(struct inode * inode,struct file * file)30 static int lmkd_dbg_trigger_proc_open(struct inode *inode, struct file *file)
31 {
32 return single_open(file, lmkd_dbg_trigger_proc_show, NULL);
33 }
34
lmkd_dbg_trigger_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)35 static ssize_t lmkd_dbg_trigger_write(struct file *file, const char __user *buf,
36 size_t count, loff_t *ppos)
37 {
38 char buffer[PROC_NUMBUF];
39 int oom_score_adj;
40 int err;
41
42 memset(buffer, 0, sizeof(buffer));
43 if (count > sizeof(buffer) - 1)
44 count = sizeof(buffer) - 1;
45 if (copy_from_user(buffer, buf, count)) {
46 err = -EFAULT;
47 goto out;
48 }
49
50 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
51 if (err)
52 goto out;
53
54 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
55 oom_score_adj > OOM_SCORE_ADJ_MAX) {
56 err = -EINVAL;
57 goto out;
58 }
59
60 lmkd_oom_score_adj = oom_score_adj;
61 lowmem_dbg(oom_score_adj);
62
63 out:
64 return err < 0 ? err : count;
65 }
66
67 static const struct proc_ops lmkd_dbg_trigger_proc_fops = {
68 .proc_open = lmkd_dbg_trigger_proc_open,
69 .proc_read = seq_read,
70 .proc_lseek = seq_lseek,
71 .proc_release = single_release,
72 .proc_write = lmkd_dbg_trigger_write,
73 };
74
proc_lmkd_dbg_trigger_init(void)75 static int __init proc_lmkd_dbg_trigger_init(void)
76 {
77 proc_create("lmkd_dbg_trigger", 0660, NULL,
78 &lmkd_dbg_trigger_proc_fops);
79 return 0;
80 }
81
82 fs_initcall(proc_lmkd_dbg_trigger_init);
83
84