proc: less code duplication in /proc/*/cmdline

After staring at this code for a while I've figured using small 2-entry
array describing ARGV and ENVP is the way to address code duplication
critique.

Link: http://lkml.kernel.org/r/20170105185724.GA12027@avx2
Signed-off-by: Alexey Dobriyan <adobriyan@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:
Alexey Dobriyan
2017-02-24 15:00:20 -08:00
committed by Linus Torvalds
parent 4e4a7fb7b4
commit a0a07b87f3

View File

@@ -291,102 +291,70 @@ static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
rv += nr_read; rv += nr_read;
} }
} else { } else {
/*
* Command line (1 string) occupies ARGV and maybe
* extends into ENVP.
*/
if (len1 + len2 <= *pos)
goto skip_argv_envp;
if (len1 <= *pos)
goto skip_argv;
p = arg_start + *pos;
len = len1 - *pos;
while (count > 0 && len > 0) {
unsigned int _count, l;
int nr_read;
bool final;
_count = min3(count, len, PAGE_SIZE);
nr_read = access_remote_vm(mm, p, page, _count, 0);
if (nr_read < 0)
rv = nr_read;
if (nr_read <= 0)
goto out_free_page;
/*
* Command line can be shorter than whole ARGV
* even if last "marker" byte says it is not.
*/
final = false;
l = strnlen(page, nr_read);
if (l < nr_read) {
nr_read = l;
final = true;
}
if (copy_to_user(buf, page, nr_read)) {
rv = -EFAULT;
goto out_free_page;
}
p += nr_read;
len -= nr_read;
buf += nr_read;
count -= nr_read;
rv += nr_read;
if (final)
goto out_free_page;
}
skip_argv:
/* /*
* Command line (1 string) occupies ARGV and * Command line (1 string) occupies ARGV and
* extends into ENVP. * extends into ENVP.
*/ */
if (len1 <= *pos) { struct {
p = env_start + *pos - len1; unsigned long p;
len = len1 + len2 - *pos; unsigned long len;
} else { } cmdline[2] = {
p = env_start; { .p = arg_start, .len = len1 },
len = len2; { .p = env_start, .len = len2 },
};
loff_t pos1 = *pos;
unsigned int i;
i = 0;
while (i < 2 && pos1 >= cmdline[i].len) {
pos1 -= cmdline[i].len;
i++;
} }
while (count > 0 && len > 0) { while (i < 2) {
unsigned int _count, l; p = cmdline[i].p + pos1;
int nr_read; len = cmdline[i].len - pos1;
bool final; while (count > 0 && len > 0) {
unsigned int _count, l;
int nr_read;
bool final;
_count = min3(count, len, PAGE_SIZE); _count = min3(count, len, PAGE_SIZE);
nr_read = access_remote_vm(mm, p, page, _count, 0); nr_read = access_remote_vm(mm, p, page, _count, 0);
if (nr_read < 0) if (nr_read < 0)
rv = nr_read; rv = nr_read;
if (nr_read <= 0) if (nr_read <= 0)
goto out_free_page; goto out_free_page;
/* Find EOS. */ /*
final = false; * Command line can be shorter than whole ARGV
l = strnlen(page, nr_read); * even if last "marker" byte says it is not.
if (l < nr_read) { */
nr_read = l; final = false;
final = true; l = strnlen(page, nr_read);
if (l < nr_read) {
nr_read = l;
final = true;
}
if (copy_to_user(buf, page, nr_read)) {
rv = -EFAULT;
goto out_free_page;
}
p += nr_read;
len -= nr_read;
buf += nr_read;
count -= nr_read;
rv += nr_read;
if (final)
goto out_free_page;
} }
if (copy_to_user(buf, page, nr_read)) { /* Only first chunk can be read partially. */
rv = -EFAULT; pos1 = 0;
goto out_free_page; i++;
}
p += nr_read;
len -= nr_read;
buf += nr_read;
count -= nr_read;
rv += nr_read;
if (final)
goto out_free_page;
} }
skip_argv_envp:
;
} }
out_free_page: out_free_page: