Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1317532 > unrolled thread

[RFC][PATCH v2] prctl: Add PR_SET_TIMERSLACK_PID for setting timer slack of an arbitrary thread.

Started byJohn Stultz <john.stultz@linaro.org>
First post2016-01-26 05:30 +0100
Last post2016-01-27 12:20 +0100
Articles 4 — 3 participants

Back to article view | Back to linux.kernel


Contents

  [RFC][PATCH v2] prctl: Add PR_SET_TIMERSLACK_PID for setting timer slack of an arbitrary thread. John Stultz <john.stultz@linaro.org> - 2016-01-26 05:30 +0100
    Re: [RFC][PATCH v2] prctl: Add PR_SET_TIMERSLACK_PID for setting  timer slack of an arbitrary thread. Arjan van de Ven <arjan@linux.intel.com> - 2016-01-26 14:40 +0100
      Re: [RFC][PATCH v2] prctl: Add PR_SET_TIMERSLACK_PID for setting  timer slack of an arbitrary thread. Thomas Gleixner <tglx@linutronix.de> - 2016-01-27 12:20 +0100
    Re: [RFC][PATCH v2] prctl: Add PR_SET_TIMERSLACK_PID for setting  timer slack of an arbitrary thread. Thomas Gleixner <tglx@linutronix.de> - 2016-01-27 12:20 +0100

#1317532 — [RFC][PATCH v2] prctl: Add PR_SET_TIMERSLACK_PID for setting timer slack of an arbitrary thread.

FromJohn Stultz <john.stultz@linaro.org>
Date2016-01-26 05:30 +0100
Subject[RFC][PATCH v2] prctl: Add PR_SET_TIMERSLACK_PID for setting timer slack of an arbitrary thread.
Message-ID<qV3qq-6cD-1@gated-at.bofh.it>
From: Ruchi Kandoi <kandoiruchi@google.com>

This allows power/performance management software to set timer
slack for other threads according to its policy for the thread
(such as when the thread is designated foreground vs. background
activity)

Second argument is similar to PR_SET_TIMERSLACK, if non-zero
then the slack is set to that value otherwise sets it to the
default for the thread.

Takes PID of the thread as the third argument.

This interface checks that the calling task has permissions to
to use PTRACE_MODE_ATTACH on the target task, so that we can
ensure arbitrary apps do not change the timer slack for other
apps.

Additional fixes from Ruchi and Micha Kalfon <micha@cellrox.com>
have been folded into this patch to make it easier to reivew.

Cc: Arjan van de Ven <arjan@linux.intel.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Oren Laadan <orenl@cellrox.com>
Cc: Micha Kalfon <micha@cellrox.com>
Cc: Ruchi Kandoi <kandoiruchi@google.com>
Cc: Rom Lemarchand <romlem@android.com>
Cc: Android Kernel Team <kernel-team@android.com>
Signed-off-by: Ruchi Kandoi <kandoiruchi@google.com>
[jstultz:
 * Folded in CAP_SYS_NICE check from Ruchi.
 * Folded in fix misplaced PR_SET_TIMERSLACK_PID case fix from
   Micha.
 * Folded in make PR_SET_TIMERSLACK_PID pid namespace aware fix
   from Micha.
 * Changed PR_SET_TIMERSLACK_PID so it didn't collide with
   already upstream prctrl values.
 * Reworked commit message.
 * Moved from CAP_SYS_NICE to PTRACE_MODE_ATTACH for permissions
   checks]
Signed-off-by: John Stultz <john.stultz@linaro.org>
---
New in v2:
* Changed from CAP_SYS_NICE to PTRACE_MODE_ATTACH permissions
  checking on Arjan's suggestion

 include/uapi/linux/prctl.h |  7 +++++++
 kernel/sys.c               | 25 +++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h
index a8d0759..1a13c2b 100644
--- a/include/uapi/linux/prctl.h
+++ b/include/uapi/linux/prctl.h
@@ -187,6 +187,13 @@ struct prctl_mm_map {
 
 #define PR_SET_FP_MODE		45
 #define PR_GET_FP_MODE		46
+
+/* Sets the timerslack for arbitrary threads
+ * arg2 slack value, 0 means "use default"
+ * arg3 pid of the thread whose timer slack needs to be set
+ */
+#define PR_SET_TIMERSLACK_PID	47
+
 # define PR_FP_MODE_FR		(1 << 0)	/* 64b FP registers */
 # define PR_FP_MODE_FRE		(1 << 1)	/* 32b compatibility */
 
diff --git a/kernel/sys.c b/kernel/sys.c
index 78947de..5189378 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -41,6 +41,9 @@
 #include <linux/syscore_ops.h>
 #include <linux/version.h>
 #include <linux/ctype.h>
+#include <linux/mm.h>
+#include <linux/mempolicy.h>
+#include <linux/sched.h>
 
 #include <linux/compat.h>
 #include <linux/syscalls.h>
@@ -2076,6 +2079,7 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 		unsigned long, arg4, unsigned long, arg5)
 {
 	struct task_struct *me = current;
+	struct task_struct *tsk;
 	unsigned char comm[sizeof(me->comm)];
 	long error;
 
@@ -2218,6 +2222,27 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 	case PR_GET_TID_ADDRESS:
 		error = prctl_get_tid_address(me, (int __user **)arg2);
 		break;
+	case PR_SET_TIMERSLACK_PID:
+		rcu_read_lock();
+		tsk = find_task_by_vpid((pid_t)arg3);
+		if (tsk == NULL) {
+			rcu_read_unlock();
+			return -EINVAL;
+		}
+		get_task_struct(tsk);
+		rcu_read_unlock();
+		if (ptrace_may_access(tsk, PTRACE_MODE_ATTACH)) {
+			put_task_struct(tsk);
+			return -EPERM;
+		}
+		if (arg2 <= 0)
+			tsk->timer_slack_ns =
+				tsk->default_timer_slack_ns;
+		else
+			tsk->timer_slack_ns = arg2;
+		put_task_struct(tsk);
+		error = 0;
+		break;
 	case PR_SET_CHILD_SUBREAPER:
 		me->signal->is_child_subreaper = !!arg2;
 		break;
-- 
1.9.1

[toc] | [next] | [standalone]


#1317918 — Re: [RFC][PATCH v2] prctl: Add PR_SET_TIMERSLACK_PID for setting timer slack of an arbitrary thread.

FromArjan van de Ven <arjan@linux.intel.com>
Date2016-01-26 14:40 +0100
SubjectRe: [RFC][PATCH v2] prctl: Add PR_SET_TIMERSLACK_PID for setting timer slack of an arbitrary thread.
Message-ID<qVc0G-4LK-17@gated-at.bofh.it>
In reply to#1317532
On 1/25/2016 8:28 PM, John Stultz wrote:
> From: Ruchi Kandoi <kandoiruchi@google.com>
>
> This allows power/performance management software to set timer
> slack for other threads according to its policy for the thread
> (such as when the thread is designated foreground vs. background
> activity)
>
> Second argument is similar to PR_SET_TIMERSLACK, if non-zero
> then the slack is set to that value otherwise sets it to the
> default for the thread.
>
> Takes PID of the thread as the third argument.
>
> This interface checks that the calling task has permissions to
> to use PTRACE_MODE_ATTACH on the target task, so that we can
> ensure arbitrary apps do not change the timer slack for other
> apps.

Acked-by: Arjan van de Ven <arjan@linux.intel.com>

only slight concern is the locking around the value of the field in the task struct,
but nobody does read-modify-write on it, so they'll get either the new or the old version,
which should be ok.

(until now only the local thread would touch the field, and if you're setting it, by definition
you're not going to sleep yet, so you're not using the field)

[toc] | [prev] | [next] | [standalone]


#1318866 — Re: [RFC][PATCH v2] prctl: Add PR_SET_TIMERSLACK_PID for setting timer slack of an arbitrary thread.

FromThomas Gleixner <tglx@linutronix.de>
Date2016-01-27 12:20 +0100
SubjectRe: [RFC][PATCH v2] prctl: Add PR_SET_TIMERSLACK_PID for setting timer slack of an arbitrary thread.
Message-ID<qVwiK-2Fz-25@gated-at.bofh.it>
In reply to#1317918
On Tue, 26 Jan 2016, Arjan van de Ven wrote:
> On 1/25/2016 8:28 PM, John Stultz wrote:
> > From: Ruchi Kandoi <kandoiruchi@google.com>
> > 
> > This allows power/performance management software to set timer
> > slack for other threads according to its policy for the thread
> > (such as when the thread is designated foreground vs. background
> > activity)
> > 
> > Second argument is similar to PR_SET_TIMERSLACK, if non-zero
> > then the slack is set to that value otherwise sets it to the
> > default for the thread.
> > 
> > Takes PID of the thread as the third argument.
> > 
> > This interface checks that the calling task has permissions to
> > to use PTRACE_MODE_ATTACH on the target task, so that we can
> > ensure arbitrary apps do not change the timer slack for other
> > apps.
> 
> Acked-by: Arjan van de Ven <arjan@linux.intel.com>
> 
> only slight concern is the locking around the value of the field in the task
> struct,
> but nobody does read-modify-write on it, so they'll get either the new or the
> old version,
> which should be ok.
> 
> (until now only the local thread would touch the field, and if you're setting
> it, by definition
> you're not going to sleep yet, so you're not using the field)

Even if you access it remote, it's a non issue. The task will see either the
old or the new value. Which is equally true when you add locking around the
write.

Thanks,

	tglx

[toc] | [prev] | [next] | [standalone]


#1318864 — Re: [RFC][PATCH v2] prctl: Add PR_SET_TIMERSLACK_PID for setting timer slack of an arbitrary thread.

FromThomas Gleixner <tglx@linutronix.de>
Date2016-01-27 12:20 +0100
SubjectRe: [RFC][PATCH v2] prctl: Add PR_SET_TIMERSLACK_PID for setting timer slack of an arbitrary thread.
Message-ID<qVwiK-2Fz-21@gated-at.bofh.it>
In reply to#1317532
On Mon, 25 Jan 2016, John Stultz wrote:
> From: Ruchi Kandoi <kandoiruchi@google.com>
> 
> This allows power/performance management software to set timer
> slack for other threads according to its policy for the thread
> (such as when the thread is designated foreground vs. background
> activity)
> 
> Second argument is similar to PR_SET_TIMERSLACK, if non-zero
> then the slack is set to that value otherwise sets it to the
> default for the thread.
> 
> Takes PID of the thread as the third argument.
> 
> This interface checks that the calling task has permissions to
> to use PTRACE_MODE_ATTACH on the target task, so that we can
> ensure arbitrary apps do not change the timer slack for other
> apps.
> 
> Additional fixes from Ruchi and Micha Kalfon <micha@cellrox.com>
> have been folded into this patch to make it easier to reivew.
> 
> Cc: Arjan van de Ven <arjan@linux.intel.com>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Oren Laadan <orenl@cellrox.com>
> Cc: Micha Kalfon <micha@cellrox.com>
> Cc: Ruchi Kandoi <kandoiruchi@google.com>
> Cc: Rom Lemarchand <romlem@android.com>
> Cc: Android Kernel Team <kernel-team@android.com>
> Signed-off-by: Ruchi Kandoi <kandoiruchi@google.com>
> [jstultz:
>  * Folded in CAP_SYS_NICE check from Ruchi.
>  * Folded in fix misplaced PR_SET_TIMERSLACK_PID case fix from
>    Micha.
>  * Folded in make PR_SET_TIMERSLACK_PID pid namespace aware fix
>    from Micha.
>  * Changed PR_SET_TIMERSLACK_PID so it didn't collide with
>    already upstream prctrl values.
>  * Reworked commit message.
>  * Moved from CAP_SYS_NICE to PTRACE_MODE_ATTACH for permissions
>    checks]
> Signed-off-by: John Stultz <john.stultz@linaro.org>

Reviewed-by: Thomas Gleixner <tglx@linutronix.de>

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web