proc: simpler iterations for /proc/*/cmdline
"rv" variable is used both as a counter of bytes transferred and an error value holder but it can be reduced solely to error values if original start of userspace buffer is stashed and used at the very end. [akpm@linux-foundation.org: simplify cleanup code] Link: http://lkml.kernel.org/r/20180221193009.GA28678@avx2 Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com> Reviewed-by: Andrew Morton <akpm@linux-foundation.org> Cc: Andy Shevchenko <andy.shevchenko@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
parent
6a6b9c4c11
commit
6a6cbe75db
1 changed files with 14 additions and 19 deletions
|
@ -215,8 +215,9 @@ static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
|
||||||
unsigned long arg_start, arg_end, env_start, env_end;
|
unsigned long arg_start, arg_end, env_start, env_end;
|
||||||
unsigned long len1, len2, len;
|
unsigned long len1, len2, len;
|
||||||
unsigned long p;
|
unsigned long p;
|
||||||
|
char __user *buf0 = buf;
|
||||||
char c;
|
char c;
|
||||||
ssize_t rv;
|
int rv;
|
||||||
|
|
||||||
BUG_ON(*pos < 0);
|
BUG_ON(*pos < 0);
|
||||||
|
|
||||||
|
@ -253,25 +254,20 @@ static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
|
||||||
len2 = env_end - env_start;
|
len2 = env_end - env_start;
|
||||||
|
|
||||||
/* Empty ARGV. */
|
/* Empty ARGV. */
|
||||||
if (len1 == 0) {
|
if (len1 == 0)
|
||||||
rv = 0;
|
goto end;
|
||||||
goto out_free_page;
|
|
||||||
}
|
|
||||||
/*
|
/*
|
||||||
* Inherently racy -- command line shares address space
|
* Inherently racy -- command line shares address space
|
||||||
* with code and data.
|
* with code and data.
|
||||||
*/
|
*/
|
||||||
if (access_remote_vm(mm, arg_end - 1, &c, 1, FOLL_ANON) != 1) {
|
if (access_remote_vm(mm, arg_end - 1, &c, 1, FOLL_ANON) != 1)
|
||||||
rv = 0;
|
goto end;
|
||||||
goto out_free_page;
|
|
||||||
}
|
|
||||||
|
|
||||||
rv = 0;
|
|
||||||
|
|
||||||
if (c == '\0') {
|
if (c == '\0') {
|
||||||
/* Command line (set of strings) occupies whole ARGV. */
|
/* Command line (set of strings) occupies whole ARGV. */
|
||||||
if (len1 <= *pos)
|
if (len1 <= *pos)
|
||||||
goto out_free_page;
|
goto end;
|
||||||
|
|
||||||
p = arg_start + *pos;
|
p = arg_start + *pos;
|
||||||
len = len1 - *pos;
|
len = len1 - *pos;
|
||||||
|
@ -281,7 +277,7 @@ static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
|
||||||
nr_read = min3(count, len, PAGE_SIZE);
|
nr_read = min3(count, len, PAGE_SIZE);
|
||||||
nr_read = access_remote_vm(mm, p, page, nr_read, FOLL_ANON);
|
nr_read = access_remote_vm(mm, p, page, nr_read, FOLL_ANON);
|
||||||
if (nr_read == 0)
|
if (nr_read == 0)
|
||||||
goto out_free_page;
|
goto end;
|
||||||
|
|
||||||
if (copy_to_user(buf, page, nr_read)) {
|
if (copy_to_user(buf, page, nr_read)) {
|
||||||
rv = -EFAULT;
|
rv = -EFAULT;
|
||||||
|
@ -292,7 +288,6 @@ static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
|
||||||
len -= nr_read;
|
len -= nr_read;
|
||||||
buf += nr_read;
|
buf += nr_read;
|
||||||
count -= nr_read;
|
count -= nr_read;
|
||||||
rv += nr_read;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/*
|
/*
|
||||||
|
@ -323,7 +318,7 @@ static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
|
||||||
nr_read = min3(count, len, PAGE_SIZE);
|
nr_read = min3(count, len, PAGE_SIZE);
|
||||||
nr_read = access_remote_vm(mm, p, page, nr_read, FOLL_ANON);
|
nr_read = access_remote_vm(mm, p, page, nr_read, FOLL_ANON);
|
||||||
if (nr_read == 0)
|
if (nr_read == 0)
|
||||||
goto out_free_page;
|
goto end;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Command line can be shorter than whole ARGV
|
* Command line can be shorter than whole ARGV
|
||||||
|
@ -340,10 +335,9 @@ static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
|
||||||
len -= nr_write;
|
len -= nr_write;
|
||||||
buf += nr_write;
|
buf += nr_write;
|
||||||
count -= nr_write;
|
count -= nr_write;
|
||||||
rv += nr_write;
|
|
||||||
|
|
||||||
if (nr_write < nr_read)
|
if (nr_write < nr_read)
|
||||||
goto out_free_page;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Only first chunk can be read partially. */
|
/* Only first chunk can be read partially. */
|
||||||
|
@ -352,12 +346,13 @@ static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
end:
|
||||||
|
*pos += buf - buf0;
|
||||||
|
rv = buf - buf0;
|
||||||
out_free_page:
|
out_free_page:
|
||||||
free_page((unsigned long)page);
|
free_page((unsigned long)page);
|
||||||
out_mmput:
|
out_mmput:
|
||||||
mmput(mm);
|
mmput(mm);
|
||||||
if (rv > 0)
|
|
||||||
*pos += rv;
|
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue