2019-05-02 19:15:40

by Joel Savitz

[permalink] [raw]
Subject: [PATCH 0/2] sys/prctl: expose TASK_SIZE value to userspace

In the mainline kernel, there is no quick mechanism to get the virtual
memory size of the current process from userspace.

Despite the current state of affairs, this information is available to the
user through several means, one being a linear search of the entire address
space. This is an inefficient use of cpu cycles.

A component of the libhugetlb kernel test does exactly this, and as
systems' address spaces increase beyond 32-bits, this method becomes
exceedingly tedious.

For example, on a ppc64le system with a 47-bit address space, the linear
search causes the test to hang for some unknown amount of time. I
couldn't give you an exact number because I just ran it for about 10-20
minutes and went to go do something else, probably to get coffee or
something, and when I came back, I just killed the test and patched it
to use this new mechanism. I re-ran my new version of the test using a
kernel with this patch, and of course it passed through the previously
bottlenecking codepath nearly instantaneously.

As such, I propose that the prctl syscall be extended to include the
option to retrieve TASK_SIZE from the kernel.

This patch will allow us to upgrade an O(n) codepath to O(1) in an
architecture-independent manner, and provide a mechanism for others
to do the same.

Joel Savitz(2):
sys/prctl: add PR_GET_TASK_SIZE option to prctl(2)
prctl.2: Document the new PR_GET_TASK_SIZE option

include/uapi/linux/prctl.h | 3 +++
kernel/sys.c | 10 ++++++++++
2 files changed, 13 insertions(+)

man2/prctl.2 | 9 +++++++++
1 file changed, 9 insertions(+)

--
2.18.1


2019-05-02 19:15:44

by Joel Savitz

[permalink] [raw]
Subject: [PATCH 1/2] kernel/sys: add PR_GET_TASK_SIZE option to prctl(2)

When PR_GET_TASK_SIZE is passed to prctl, the kernel will attempt to
copy the value of TASK_SIZE to the userspace address in arg2.

Suggested-by: Alexey Dobriyan <[email protected]>
Signed-off-by: Joel Savitz <[email protected]>
---
include/uapi/linux/prctl.h | 3 +++
kernel/sys.c | 10 ++++++++++
2 files changed, 13 insertions(+)

diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h
index 094bb03b9cc2..2335fe0a8db8 100644
--- a/include/uapi/linux/prctl.h
+++ b/include/uapi/linux/prctl.h
@@ -229,4 +229,7 @@ struct prctl_mm_map {
# define PR_PAC_APDBKEY (1UL << 3)
# define PR_PAC_APGAKEY (1UL << 4)

+/* Get the process virtual memory size */
+#define PR_GET_TASK_SIZE 55
+
#endif /* _LINUX_PRCTL_H */
diff --git a/kernel/sys.c b/kernel/sys.c
index 12df0e5434b8..7ced7dbd035d 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -2252,6 +2252,13 @@ static int propagate_has_child_subreaper(struct task_struct *p, void *data)
return 1;
}

+static int prctl_get_tasksize(void __user * uaddr)
+{
+ unsigned long long task_size = TASK_SIZE;
+ return copy_to_user(uaddr, &task_size, sizeof(unsigned long long))
+ ? -EFAULT : 0;
+}
+
int __weak arch_prctl_spec_ctrl_get(struct task_struct *t, unsigned long which)
{
return -EINVAL;
@@ -2486,6 +2493,9 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
return -EINVAL;
error = PAC_RESET_KEYS(me, arg2);
break;
+ case PR_GET_TASK_SIZE:
+ error = prctl_get_tasksize((void *)arg2) ;
+ break;
default:
error = -EINVAL;
break;
--
2.18.1

2019-05-02 19:16:41

by Joel Savitz

[permalink] [raw]
Subject: [PATCH 2/2] prctl.2: Document the new PR_GET_TASK_SIZE option

Add a short explanation of the new PR_GET_TASK_SIZE option for the benefit
of future generations.

Signed-off-by: Joel Savitz <[email protected]>
---
man2/prctl.2 | 9 +++++++++
1 file changed, 9 insertions(+)

diff --git a/man2/prctl.2 b/man2/prctl.2
index 06d8e13c7..35a6a3919 100644
--- a/man2/prctl.2
+++ b/man2/prctl.2
@@ -49,6 +49,7 @@
.\" 2013-01-10 Kees Cook, document PR_SET_PTRACER
.\" 2012-02-04 Michael Kerrisk, document PR_{SET,GET}_CHILD_SUBREAPER
.\" 2014-11-10 Dave Hansen, document PR_MPX_{EN,DIS}ABLE_MANAGEMENT
+.\" 2019-05-02 Joel Savitz, document PR_GET_TASK_SIZE
.\"
.\"
.TH PRCTL 2 2019-03-06 "Linux" "Linux Programmer's Manual"
@@ -1375,6 +1376,14 @@ system call on Tru64).
for information on versions and architectures)
Return unaligned access control bits, in the location pointed to by
.IR "(unsigned int\ *) arg2" .
+.TP
+.B PR_GET_TASK_SIZE
+Copy the value of TASK_SIZE to the userspace address in
+.IR "arg2" .
+Return
+.B EFAULT
+if this operation fails.
+
.SH RETURN VALUE
On success,
.BR PR_GET_DUMPABLE ,
--
2.18.1

2019-05-03 11:25:47

by David Laight

[permalink] [raw]
Subject: RE: [PATCH 2/2] prctl.2: Document the new PR_GET_TASK_SIZE option

From: Joel Savitz
> Sent: 02 May 2019 20:13
> Add a short explanation of the new PR_GET_TASK_SIZE option for the benefit
> of future generations.
>
> Signed-off-by: Joel Savitz <[email protected]>
> ---
> man2/prctl.2 | 9 +++++++++
> 1 file changed, 9 insertions(+)
>
> diff --git a/man2/prctl.2 b/man2/prctl.2
> index 06d8e13c7..35a6a3919 100644
> --- a/man2/prctl.2
> +++ b/man2/prctl.2
> @@ -49,6 +49,7 @@
> .\" 2013-01-10 Kees Cook, document PR_SET_PTRACER
> .\" 2012-02-04 Michael Kerrisk, document PR_{SET,GET}_CHILD_SUBREAPER
> .\" 2014-11-10 Dave Hansen, document PR_MPX_{EN,DIS}ABLE_MANAGEMENT
> +.\" 2019-05-02 Joel Savitz, document PR_GET_TASK_SIZE
> .\"
> .\"
> .TH PRCTL 2 2019-03-06 "Linux" "Linux Programmer's Manual"
> @@ -1375,6 +1376,14 @@ system call on Tru64).
> for information on versions and architectures)
> Return unaligned access control bits, in the location pointed to by
> .IR "(unsigned int\ *) arg2" .
> +.TP
> +.B PR_GET_TASK_SIZE
> +Copy the value of TASK_SIZE to the userspace address in
> +.IR "arg2" .
> +Return
> +.B EFAULT
> +if this operation fails.
> +

Shouldn't this say what the value is?
ISTR a recent patch to change the was the 'used to be constant' TASK_SIZE is defined.
I think it might be 'The highest userspace virtual address the current
process can use.' But I might be wrong.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

2019-05-03 16:05:44

by Joel Savitz

[permalink] [raw]
Subject: Re: [PATCH 2/2] prctl.2: Document the new PR_GET_TASK_SIZE option

On Fri, May 3, 2019 at 7:20 AM David Laight <[email protected]> wrote:
> Shouldn't this say what the value is?
> ISTR a recent patch to change the was the 'used to be constant' TASK_SIZE is defined.
> I think it might be 'The highest userspace virtual address the current
> process can use.' But I might be wrong.

I believe you are correct David. I will add this information to the
manpage in the upcoming v3.
Best,
Joel Savitz