diff options
Diffstat (limited to 'fs/proc/base.c')
-rw-r--r-- | fs/proc/base.c | 120 |
1 files changed, 111 insertions, 9 deletions
diff --git a/fs/proc/base.c b/fs/proc/base.c index acb7ef80ea4f..c806dfb24e08 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c | |||
@@ -63,6 +63,7 @@ | |||
63 | #include <linux/namei.h> | 63 | #include <linux/namei.h> |
64 | #include <linux/mnt_namespace.h> | 64 | #include <linux/mnt_namespace.h> |
65 | #include <linux/mm.h> | 65 | #include <linux/mm.h> |
66 | #include <linux/swap.h> | ||
66 | #include <linux/rcupdate.h> | 67 | #include <linux/rcupdate.h> |
67 | #include <linux/kallsyms.h> | 68 | #include <linux/kallsyms.h> |
68 | #include <linux/stacktrace.h> | 69 | #include <linux/stacktrace.h> |
@@ -427,17 +428,14 @@ static const struct file_operations proc_lstats_operations = { | |||
427 | 428 | ||
428 | #endif | 429 | #endif |
429 | 430 | ||
430 | /* The badness from the OOM killer */ | ||
431 | unsigned long badness(struct task_struct *p, unsigned long uptime); | ||
432 | static int proc_oom_score(struct task_struct *task, char *buffer) | 431 | static int proc_oom_score(struct task_struct *task, char *buffer) |
433 | { | 432 | { |
434 | unsigned long points = 0; | 433 | unsigned long points = 0; |
435 | struct timespec uptime; | ||
436 | 434 | ||
437 | do_posix_clock_monotonic_gettime(&uptime); | ||
438 | read_lock(&tasklist_lock); | 435 | read_lock(&tasklist_lock); |
439 | if (pid_alive(task)) | 436 | if (pid_alive(task)) |
440 | points = badness(task, uptime.tv_sec); | 437 | points = oom_badness(task, NULL, NULL, |
438 | totalram_pages + total_swap_pages); | ||
441 | read_unlock(&tasklist_lock); | 439 | read_unlock(&tasklist_lock); |
442 | return sprintf(buffer, "%lu\n", points); | 440 | return sprintf(buffer, "%lu\n", points); |
443 | } | 441 | } |
@@ -561,9 +559,19 @@ static int proc_setattr(struct dentry *dentry, struct iattr *attr) | |||
561 | return -EPERM; | 559 | return -EPERM; |
562 | 560 | ||
563 | error = inode_change_ok(inode, attr); | 561 | error = inode_change_ok(inode, attr); |
564 | if (!error) | 562 | if (error) |
565 | error = inode_setattr(inode, attr); | 563 | return error; |
566 | return error; | 564 | |
565 | if ((attr->ia_valid & ATTR_SIZE) && | ||
566 | attr->ia_size != i_size_read(inode)) { | ||
567 | error = vmtruncate(inode, attr->ia_size); | ||
568 | if (error) | ||
569 | return error; | ||
570 | } | ||
571 | |||
572 | setattr_copy(inode, attr); | ||
573 | mark_inode_dirty(inode); | ||
574 | return 0; | ||
567 | } | 575 | } |
568 | 576 | ||
569 | static const struct inode_operations proc_def_inode_operations = { | 577 | static const struct inode_operations proc_def_inode_operations = { |
@@ -1039,8 +1047,24 @@ static ssize_t oom_adjust_write(struct file *file, const char __user *buf, | |||
1039 | return -EACCES; | 1047 | return -EACCES; |
1040 | } | 1048 | } |
1041 | 1049 | ||
1050 | /* | ||
1051 | * Warn that /proc/pid/oom_adj is deprecated, see | ||
1052 | * Documentation/feature-removal-schedule.txt. | ||
1053 | */ | ||
1054 | printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, " | ||
1055 | "please use /proc/%d/oom_score_adj instead.\n", | ||
1056 | current->comm, task_pid_nr(current), | ||
1057 | task_pid_nr(task), task_pid_nr(task)); | ||
1042 | task->signal->oom_adj = oom_adjust; | 1058 | task->signal->oom_adj = oom_adjust; |
1043 | 1059 | /* | |
1060 | * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum | ||
1061 | * value is always attainable. | ||
1062 | */ | ||
1063 | if (task->signal->oom_adj == OOM_ADJUST_MAX) | ||
1064 | task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX; | ||
1065 | else | ||
1066 | task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) / | ||
1067 | -OOM_DISABLE; | ||
1044 | unlock_task_sighand(task, &flags); | 1068 | unlock_task_sighand(task, &flags); |
1045 | put_task_struct(task); | 1069 | put_task_struct(task); |
1046 | 1070 | ||
@@ -1053,6 +1077,82 @@ static const struct file_operations proc_oom_adjust_operations = { | |||
1053 | .llseek = generic_file_llseek, | 1077 | .llseek = generic_file_llseek, |
1054 | }; | 1078 | }; |
1055 | 1079 | ||
1080 | static ssize_t oom_score_adj_read(struct file *file, char __user *buf, | ||
1081 | size_t count, loff_t *ppos) | ||
1082 | { | ||
1083 | struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); | ||
1084 | char buffer[PROC_NUMBUF]; | ||
1085 | int oom_score_adj = OOM_SCORE_ADJ_MIN; | ||
1086 | unsigned long flags; | ||
1087 | size_t len; | ||
1088 | |||
1089 | if (!task) | ||
1090 | return -ESRCH; | ||
1091 | if (lock_task_sighand(task, &flags)) { | ||
1092 | oom_score_adj = task->signal->oom_score_adj; | ||
1093 | unlock_task_sighand(task, &flags); | ||
1094 | } | ||
1095 | put_task_struct(task); | ||
1096 | len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj); | ||
1097 | return simple_read_from_buffer(buf, count, ppos, buffer, len); | ||
1098 | } | ||
1099 | |||
1100 | static ssize_t oom_score_adj_write(struct file *file, const char __user *buf, | ||
1101 | size_t count, loff_t *ppos) | ||
1102 | { | ||
1103 | struct task_struct *task; | ||
1104 | char buffer[PROC_NUMBUF]; | ||
1105 | unsigned long flags; | ||
1106 | long oom_score_adj; | ||
1107 | int err; | ||
1108 | |||
1109 | memset(buffer, 0, sizeof(buffer)); | ||
1110 | if (count > sizeof(buffer) - 1) | ||
1111 | count = sizeof(buffer) - 1; | ||
1112 | if (copy_from_user(buffer, buf, count)) | ||
1113 | return -EFAULT; | ||
1114 | |||
1115 | err = strict_strtol(strstrip(buffer), 0, &oom_score_adj); | ||
1116 | if (err) | ||
1117 | return -EINVAL; | ||
1118 | if (oom_score_adj < OOM_SCORE_ADJ_MIN || | ||
1119 | oom_score_adj > OOM_SCORE_ADJ_MAX) | ||
1120 | return -EINVAL; | ||
1121 | |||
1122 | task = get_proc_task(file->f_path.dentry->d_inode); | ||
1123 | if (!task) | ||
1124 | return -ESRCH; | ||
1125 | if (!lock_task_sighand(task, &flags)) { | ||
1126 | put_task_struct(task); | ||
1127 | return -ESRCH; | ||
1128 | } | ||
1129 | if (oom_score_adj < task->signal->oom_score_adj && | ||
1130 | !capable(CAP_SYS_RESOURCE)) { | ||
1131 | unlock_task_sighand(task, &flags); | ||
1132 | put_task_struct(task); | ||
1133 | return -EACCES; | ||
1134 | } | ||
1135 | |||
1136 | task->signal->oom_score_adj = oom_score_adj; | ||
1137 | /* | ||
1138 | * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is | ||
1139 | * always attainable. | ||
1140 | */ | ||
1141 | if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN) | ||
1142 | task->signal->oom_adj = OOM_DISABLE; | ||
1143 | else | ||
1144 | task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) / | ||
1145 | OOM_SCORE_ADJ_MAX; | ||
1146 | unlock_task_sighand(task, &flags); | ||
1147 | put_task_struct(task); | ||
1148 | return count; | ||
1149 | } | ||
1150 | |||
1151 | static const struct file_operations proc_oom_score_adj_operations = { | ||
1152 | .read = oom_score_adj_read, | ||
1153 | .write = oom_score_adj_write, | ||
1154 | }; | ||
1155 | |||
1056 | #ifdef CONFIG_AUDITSYSCALL | 1156 | #ifdef CONFIG_AUDITSYSCALL |
1057 | #define TMPBUFLEN 21 | 1157 | #define TMPBUFLEN 21 |
1058 | static ssize_t proc_loginuid_read(struct file * file, char __user * buf, | 1158 | static ssize_t proc_loginuid_read(struct file * file, char __user * buf, |
@@ -2625,6 +2725,7 @@ static const struct pid_entry tgid_base_stuff[] = { | |||
2625 | #endif | 2725 | #endif |
2626 | INF("oom_score", S_IRUGO, proc_oom_score), | 2726 | INF("oom_score", S_IRUGO, proc_oom_score), |
2627 | REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), | 2727 | REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), |
2728 | REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), | ||
2628 | #ifdef CONFIG_AUDITSYSCALL | 2729 | #ifdef CONFIG_AUDITSYSCALL |
2629 | REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), | 2730 | REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), |
2630 | REG("sessionid", S_IRUGO, proc_sessionid_operations), | 2731 | REG("sessionid", S_IRUGO, proc_sessionid_operations), |
@@ -2959,6 +3060,7 @@ static const struct pid_entry tid_base_stuff[] = { | |||
2959 | #endif | 3060 | #endif |
2960 | INF("oom_score", S_IRUGO, proc_oom_score), | 3061 | INF("oom_score", S_IRUGO, proc_oom_score), |
2961 | REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), | 3062 | REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), |
3063 | REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), | ||
2962 | #ifdef CONFIG_AUDITSYSCALL | 3064 | #ifdef CONFIG_AUDITSYSCALL |
2963 | REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), | 3065 | REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), |
2964 | REG("sessionid", S_IRUSR, proc_sessionid_operations), | 3066 | REG("sessionid", S_IRUSR, proc_sessionid_operations), |