linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] sys/prctl: expose TASK_SIZE value to userspace
@ 2019-05-02 19:13 Joel Savitz
  2019-05-02 19:13 ` [PATCH 1/2] kernel/sys: add PR_GET_TASK_SIZE option to prctl(2) Joel Savitz
  2019-05-02 19:13 ` [PATCH 2/2] prctl.2: Document the new PR_GET_TASK_SIZE option Joel Savitz
  0 siblings, 2 replies; 5+ messages in thread
From: Joel Savitz @ 2019-05-02 19:13 UTC (permalink / raw)
  To: linux-kernel
  Cc: Joel Savitz, Thomas Gleixner, Ingo Molnar, Masami Hiramatsu,
	Waiman Long, Mauro Carvalho Chehab, Kristina Martsenko,
	Andrew Morton, Cyrill Gorcunov, Kees Cook, Gustavo A. R. Silva,
	YueHaibing, Micah Morton, Yang Shi, Jann Horn, Alexey Dobriyan,
	Rafael Aquini, Michael Kerrisk

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] kernel/sys: add PR_GET_TASK_SIZE option to prctl(2)
  2019-05-02 19:13 [PATCH 0/2] sys/prctl: expose TASK_SIZE value to userspace Joel Savitz
@ 2019-05-02 19:13 ` Joel Savitz
  2019-05-02 19:13 ` [PATCH 2/2] prctl.2: Document the new PR_GET_TASK_SIZE option Joel Savitz
  1 sibling, 0 replies; 5+ messages in thread
From: Joel Savitz @ 2019-05-02 19:13 UTC (permalink / raw)
  To: linux-kernel
  Cc: Joel Savitz, Thomas Gleixner, Ingo Molnar, Masami Hiramatsu,
	Waiman Long, Mauro Carvalho Chehab, Kristina Martsenko,
	Andrew Morton, Cyrill Gorcunov, Kees Cook, Gustavo A. R. Silva,
	YueHaibing, Micah Morton, Yang Shi, Jann Horn, Alexey Dobriyan,
	Rafael Aquini, Michael Kerrisk

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 <adobriyan@gmail.com>
Signed-off-by: Joel Savitz <jsavitz@redhat.com>
---
 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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] prctl.2: Document the new PR_GET_TASK_SIZE option
  2019-05-02 19:13 [PATCH 0/2] sys/prctl: expose TASK_SIZE value to userspace Joel Savitz
  2019-05-02 19:13 ` [PATCH 1/2] kernel/sys: add PR_GET_TASK_SIZE option to prctl(2) Joel Savitz
@ 2019-05-02 19:13 ` Joel Savitz
  2019-05-03 11:20   ` David Laight
  1 sibling, 1 reply; 5+ messages in thread
From: Joel Savitz @ 2019-05-02 19:13 UTC (permalink / raw)
  To: linux-kernel
  Cc: Joel Savitz, Thomas Gleixner, Ingo Molnar, Masami Hiramatsu,
	Waiman Long, Mauro Carvalho Chehab, Kristina Martsenko,
	Andrew Morton, Cyrill Gorcunov, Kees Cook, Gustavo A. R. Silva,
	YueHaibing, Micah Morton, Yang Shi, Jann Horn, Alexey Dobriyan,
	Rafael Aquini, Michael Kerrisk

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

Signed-off-by: Joel Savitz <jsavitz@redhat.com>
---
 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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* RE: [PATCH 2/2] prctl.2: Document the new PR_GET_TASK_SIZE option
  2019-05-02 19:13 ` [PATCH 2/2] prctl.2: Document the new PR_GET_TASK_SIZE option Joel Savitz
@ 2019-05-03 11:20   ` David Laight
  2019-05-03 15:40     ` Joel Savitz
  0 siblings, 1 reply; 5+ messages in thread
From: David Laight @ 2019-05-03 11:20 UTC (permalink / raw)
  To: 'Joel Savitz', linux-kernel
  Cc: Thomas Gleixner, Ingo Molnar, Masami Hiramatsu, Waiman Long,
	Mauro Carvalho Chehab, Kristina Martsenko, Andrew Morton,
	Cyrill Gorcunov, Kees Cook, Gustavo A. R. Silva, YueHaibing,
	Micah Morton, Yang Shi, Jann Horn, Alexey Dobriyan,
	Rafael Aquini, Michael Kerrisk

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 <jsavitz@redhat.com>
> ---
>  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)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] prctl.2: Document the new PR_GET_TASK_SIZE option
  2019-05-03 11:20   ` David Laight
@ 2019-05-03 15:40     ` Joel Savitz
  0 siblings, 0 replies; 5+ messages in thread
From: Joel Savitz @ 2019-05-03 15:40 UTC (permalink / raw)
  To: David Laight
  Cc: linux-kernel, Thomas Gleixner, Ingo Molnar, Masami Hiramatsu,
	Waiman Long, Mauro Carvalho Chehab, Kristina Martsenko,
	Andrew Morton, Cyrill Gorcunov, Kees Cook, Gustavo A. R. Silva,
	YueHaibing, Micah Morton, Yang Shi, Jann Horn, Alexey Dobriyan,
	Rafael Aquini, Michael Kerrisk

On Fri, May 3, 2019 at 7:20 AM David Laight <David.Laight@aculab.com> 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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-05-03 15:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-02 19:13 [PATCH 0/2] sys/prctl: expose TASK_SIZE value to userspace Joel Savitz
2019-05-02 19:13 ` [PATCH 1/2] kernel/sys: add PR_GET_TASK_SIZE option to prctl(2) Joel Savitz
2019-05-02 19:13 ` [PATCH 2/2] prctl.2: Document the new PR_GET_TASK_SIZE option Joel Savitz
2019-05-03 11:20   ` David Laight
2019-05-03 15:40     ` Joel Savitz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).