aboutsummaryrefslogtreecommitdiffstats
path: root/fs/proc/base.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/proc/base.c')
-rw-r--r--fs/proc/base.c144
1 files changed, 123 insertions, 21 deletions
diff --git a/fs/proc/base.c b/fs/proc/base.c
index acb7ef80ea4f..a1c43e7c8a7b 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>
@@ -148,18 +149,13 @@ static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
148 return count; 149 return count;
149} 150}
150 151
151static int get_fs_path(struct task_struct *task, struct path *path, bool root) 152static int get_task_root(struct task_struct *task, struct path *root)
152{ 153{
153 struct fs_struct *fs;
154 int result = -ENOENT; 154 int result = -ENOENT;
155 155
156 task_lock(task); 156 task_lock(task);
157 fs = task->fs; 157 if (task->fs) {
158 if (fs) { 158 get_fs_root(task->fs, root);
159 read_lock(&fs->lock);
160 *path = root ? fs->root : fs->pwd;
161 path_get(path);
162 read_unlock(&fs->lock);
163 result = 0; 159 result = 0;
164 } 160 }
165 task_unlock(task); 161 task_unlock(task);
@@ -172,7 +168,12 @@ static int proc_cwd_link(struct inode *inode, struct path *path)
172 int result = -ENOENT; 168 int result = -ENOENT;
173 169
174 if (task) { 170 if (task) {
175 result = get_fs_path(task, path, 0); 171 task_lock(task);
172 if (task->fs) {
173 get_fs_pwd(task->fs, path);
174 result = 0;
175 }
176 task_unlock(task);
176 put_task_struct(task); 177 put_task_struct(task);
177 } 178 }
178 return result; 179 return result;
@@ -184,7 +185,7 @@ static int proc_root_link(struct inode *inode, struct path *path)
184 int result = -ENOENT; 185 int result = -ENOENT;
185 186
186 if (task) { 187 if (task) {
187 result = get_fs_path(task, path, 1); 188 result = get_task_root(task, path);
188 put_task_struct(task); 189 put_task_struct(task);
189 } 190 }
190 return result; 191 return result;
@@ -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 */
431unsigned long badness(struct task_struct *p, unsigned long uptime);
432static int proc_oom_score(struct task_struct *task, char *buffer) 431static 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
569static const struct inode_operations proc_def_inode_operations = { 577static const struct inode_operations proc_def_inode_operations = {
@@ -589,7 +597,7 @@ static int mounts_open_common(struct inode *inode, struct file *file,
589 get_mnt_ns(ns); 597 get_mnt_ns(ns);
590 } 598 }
591 rcu_read_unlock(); 599 rcu_read_unlock();
592 if (ns && get_fs_path(task, &root, 1) == 0) 600 if (ns && get_task_root(task, &root) == 0)
593 ret = 0; 601 ret = 0;
594 put_task_struct(task); 602 put_task_struct(task);
595 } 603 }
@@ -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
1080static 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
1100static 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
1151static 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
1058static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 1158static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
@@ -1426,7 +1526,7 @@ static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1426 if (!tmp) 1526 if (!tmp)
1427 return -ENOMEM; 1527 return -ENOMEM;
1428 1528
1429 pathname = d_path(path, tmp, PAGE_SIZE); 1529 pathname = d_path_with_unreachable(path, tmp, PAGE_SIZE);
1430 len = PTR_ERR(pathname); 1530 len = PTR_ERR(pathname);
1431 if (IS_ERR(pathname)) 1531 if (IS_ERR(pathname))
1432 goto out; 1532 goto out;
@@ -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),