Skip to contentSkip to conversation

sys/prctl: expose TASK_SIZE value to userspace

PATCH Superseded Joel Savitz 2 participants 5 messages May 2, 2019 – May 3, 2019

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover 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

#1 lore raw

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.

---
 include/uapi/linux/prctl.h |  3 +++
 kernel/sys.c               | 10 ++++++++++
 2 files changed, 13 insertions(+)
include/uapi/linux/prctl.h +3 -0
index 094bb03b9cc2..2335fe0a8db8 100644
@@ -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 */
kernel/sys.c +10 -0
index 12df0e5434b8..7ced7dbd035d 100644
@@ -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;
Signature · 1 line
2.18.1
#2 lore raw

PATCH 2/2 prctl.2: Document the new PR_GET_TASK_SIZE option

In reply to Joel Savitz

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

---
 man2/prctl.2 | 9 +++++++++
 1 file changed, 9 insertions(+)
man2/prctl.2 +9 -0
index 06d8e13c7..35a6a3919 100644
@@ -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 ,
Signature · 1 line
2.18.1
#3 lore raw

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

From: Joel Savitz

Quoted 23 lines

Sent: 02 May 2019 20:13
Add a short explanation of the new PR_GET_TASK_SIZE option for the benefit of future generations.

---
 man2/prctl.2 | 9 +++++++++
 1 file changed, 9 insertions(+)
man2/prctl.2 +1 -0
index 06d8e13c7..35a6a3919 100644
@@ -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)

man2/prctl.2 system call on Tru64). · line 1386

Quoted 10 lines
man2/prctl.2 +8 -0
 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)

#4 lore raw

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

Quoted 4 lines · David Laight

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