Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp3849992pxu; Tue, 20 Oct 2020 02:01:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJySvQ7eU7mYlI54ka/Of14g8D55TYEs24o2QSU5HsJs9gDPm9oPTQMVkQkySi5di7sqmFBV X-Received: by 2002:aa7:dd11:: with SMTP id i17mr1719532edv.188.1603184462005; Tue, 20 Oct 2020 02:01:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603184461; cv=none; d=google.com; s=arc-20160816; b=WnkiqLN5zBvCgXthDU7+wGMBDj7uQmjjVpTbxLBarzdwW2ncfBvp/+BWsIfm7hWZPy wkQndZjmsZZLOG5ZeLp7LbgVf2Ck5pi5EBWLUYvA1ZWBs5o2yN7+mKJNGdpSXpA4l+N/ +QyAhM1ZN/Qdzh7yvzRx+o5XTyMdMKtyH7SLlpsODJlPQpH49DQ+sZhnXB9jBmGVzrsu U6wwiyhMQfJC7UThNvU5w/FuP2616/G8A4sxA8ExBKNq9H8/0jU4DYxk2jq+hi6znfCb Iw4XTy4BtOarkg31Ae9skLrYgpV28ar+NuIUoZn0AJA1PDlJIPVdfPXNxq+2qT6Cyc8w iJfg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=f3SgsUK223+FY6bJR4gSyQVMXeQ4H0d1HsyCtBOHPqw=; b=r6Xh9DUptLIDE1fP/V3+cqSh0ga0NDd0Jdga4UR2ywmKBYWbA0rz+mKBWBvzPrVD9a 9zd+CRRh2OB/KBOBsksOANzzGjGZtzHrka2VaYHAg4EsQUTGYrX4X3fYLkfj2/0z/7QC 2Y1rmJoAuCnm7yKGx1bVGhrXdLVBMGdg6k3iRsc3njF2CMMrzNrIDyW1mxgPewBMg04Y F5YDlKsHbJn7i/A8FP00nXceArn7DVNiKDJv2puFgSElYpAIPWfMgEeXS6JU2V4PYohz 3zXkUwLgxff/VUYuCN9clMOBXO2zWs9VQZ4VfWbElHPMTyCg8/b9tVvA4f4Ctg/14x50 HAmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=TlO5nj34; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k18si735486edr.149.2020.10.20.02.00.39; Tue, 20 Oct 2020 02:01:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=TlO5nj34; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729372AbgJTBor (ORCPT + 99 others); Mon, 19 Oct 2020 21:44:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390246AbgJTBok (ORCPT ); Mon, 19 Oct 2020 21:44:40 -0400 Received: from mail-io1-xd44.google.com (mail-io1-xd44.google.com [IPv6:2607:f8b0:4864:20::d44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48CE6C0613CE for ; Mon, 19 Oct 2020 18:44:40 -0700 (PDT) Received: by mail-io1-xd44.google.com with SMTP id k6so593544ior.2 for ; Mon, 19 Oct 2020 18:44:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=f3SgsUK223+FY6bJR4gSyQVMXeQ4H0d1HsyCtBOHPqw=; b=TlO5nj34zgGg58rYhyDRNX0zsp1+srEi1hxq/zZUf1nWA2yyLd73GQ/Km5dMCzzYcS 46GuPbVh7mSdoiYpOcxjo10bM/f3Vj6Es1FtemE+tTvAMUk8KFaSSFqmTh0anhyjFsML fJw5/FIhF+qK0RpklYZ5hyJUnBLdFTnEzLXT8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=f3SgsUK223+FY6bJR4gSyQVMXeQ4H0d1HsyCtBOHPqw=; b=iO1/F7r9XyDLKTNIqS6IaWl2vD9q0RJHX+R8iL1OWlY3Ngh2ypLPxUgPDWwpBwzGNN vRH+UgiWhBUlIDQFDWJ76Iwg2HxoCrzZs0Foe2ozCXODPJaIxmvVRn4ZtcWejLC/5wTU ATHgkCTSC6c/CAGGtexM2u+j1ZrmqGJM/9qHcQayHueP+68gYqNVp4sCUlWrm/iiwvrq wwnUn86ip5WrYv2XE+XTbL4yeeDiLse2KZew3vsJm9HSN35omaOaqxmzENtA+ElS7ACX wuFgxFYtH9yrma7ODaw/hQqGrT8NqgjydjZH5iyuIQvg7GdOFxilESAtAUK5XLaIc/Hf /cig== X-Gm-Message-State: AOAM530VW16YQ6sCgzBcyQnjOy42VZ1w3YXr7L4iiFPyMeKjYzHp/+S/ 2JAYn/HKnzhbhaufEEWnLOf2TPyBX6KfNw== X-Received: by 2002:a6b:8f92:: with SMTP id r140mr462094iod.81.1603158279651; Mon, 19 Oct 2020 18:44:39 -0700 (PDT) Received: from joelaf.cam.corp.google.com ([2620:15c:6:12:cad3:ffff:feb3:bd59]) by smtp.gmail.com with ESMTPSA id t22sm485306ili.9.2020.10.19.18.44.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Oct 2020 18:44:38 -0700 (PDT) From: "Joel Fernandes (Google)" To: Nishanth Aravamudan , Julien Desfossez , Peter Zijlstra , Tim Chen , Vineeth Pillai , Aaron Lu , Aubrey Li , tglx@linutronix.de, linux-kernel@vger.kernel.org Cc: mingo@kernel.org, torvalds@linux-foundation.org, fweisbec@gmail.com, keescook@chromium.org, kerrnel@google.com, Phil Auld , Valentin Schneider , Mel Gorman , Pawan Gupta , Paolo Bonzini , joel@joelfernandes.org, vineeth@bitbyteword.org, Chen Yu , Christian Brauner , Agata Gruza , Antonio Gomez Iglesias , graf@amazon.com, konrad.wilk@oracle.com, dfaggioli@suse.com, pjt@google.com, rostedt@goodmis.org, derkling@google.com, benbjiang@tencent.com, Alexandre Chartre , James.Bottomley@hansenpartnership.com, OWeisse@umich.edu, Dhaval Giani , Junaid Shahid , jsbarnes@google.com, chris.hyser@oracle.com, Aubrey Li , "Paul E. McKenney" , Tim Chen Subject: [PATCH v8 -tip 18/26] sched: Add a per-thread core scheduling interface Date: Mon, 19 Oct 2020 21:43:28 -0400 Message-Id: <20201020014336.2076526-19-joel@joelfernandes.org> X-Mailer: git-send-email 2.29.0.rc1.297.gfa9743e501-goog In-Reply-To: <20201020014336.2076526-1-joel@joelfernandes.org> References: <20201020014336.2076526-1-joel@joelfernandes.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a per-thread core scheduling interface which allows a thread to share a core with another thread, or have a core exclusively for itself. ChromeOS uses core-scheduling to securely enable hyperthreading. This cuts down the keypress latency in Google docs from 150ms to 50ms while improving the camera streaming frame rate by ~3%. Tested-by: Julien Desfossez Signed-off-by: Joel Fernandes (Google) --- include/linux/sched.h | 2 ++ include/uapi/linux/prctl.h | 3 ++ kernel/sched/core.c | 51 +++++++++++++++++++++++++++++--- kernel/sys.c | 3 ++ tools/include/uapi/linux/prctl.h | 3 ++ 5 files changed, 58 insertions(+), 4 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index c6034c00846a..4cb76575afa8 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -2078,11 +2078,13 @@ void sched_core_unsafe_enter(void); void sched_core_unsafe_exit(void); bool sched_core_wait_till_safe(unsigned long ti_check); bool sched_core_kernel_protected(void); +int sched_core_share_pid(pid_t pid); #else #define sched_core_unsafe_enter(ignore) do { } while (0) #define sched_core_unsafe_exit(ignore) do { } while (0) #define sched_core_wait_till_safe(ignore) do { } while (0) #define sched_core_kernel_protected(ignore) do { } while (0) +#define sched_core_share_pid(pid_t pid) do { } while (0) #endif #endif diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index c334e6a02e5f..217b0482aea1 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -248,4 +248,7 @@ struct prctl_mm_map { #define PR_SET_IO_FLUSHER 57 #define PR_GET_IO_FLUSHER 58 +/* Request the scheduler to share a core */ +#define PR_SCHED_CORE_SHARE 59 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 30a9e4cb5ce1..a0678614a056 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -310,6 +310,7 @@ static int __sched_core_stopper(void *data) } static DEFINE_MUTEX(sched_core_mutex); +static DEFINE_MUTEX(sched_core_tasks_mutex); static int sched_core_count; static void __sched_core_enable(void) @@ -3588,8 +3589,9 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p) RB_CLEAR_NODE(&p->core_node); /* - * Tag child via per-task cookie only if parent is tagged via per-task - * cookie. This is independent of, but can be additive to the CGroup tagging. + * If parent is tagged via per-task cookie, tag the child (either with + * the parent's cookie, or a new one). The final cookie is calculated + * by concatenating the per-task cookie with that of the CGroup's. */ if (current->core_task_cookie) { @@ -9301,7 +9303,7 @@ static int sched_core_share_tasks(struct task_struct *t1, struct task_struct *t2 unsigned long cookie; int ret = -ENOMEM; - mutex_lock(&sched_core_mutex); + mutex_lock(&sched_core_tasks_mutex); /* * NOTE: sched_core_get() is done by sched_core_alloc_task_cookie() or @@ -9400,10 +9402,51 @@ static int sched_core_share_tasks(struct task_struct *t1, struct task_struct *t2 ret = 0; out_unlock: - mutex_unlock(&sched_core_mutex); + mutex_unlock(&sched_core_tasks_mutex); return ret; } +/* Called from prctl interface: PR_SCHED_CORE_SHARE */ +int sched_core_share_pid(pid_t pid) +{ + struct task_struct *task; + int err; + + if (pid == 0) { /* Recent current task's cookie. */ + /* Resetting a cookie requires privileges. */ + if (current->core_task_cookie) + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + task = NULL; + } else { + rcu_read_lock(); + task = pid ? find_task_by_vpid(pid) : current; + if (!task) { + rcu_read_unlock(); + return -ESRCH; + } + + get_task_struct(task); + + /* + * Check if this process has the right to modify the specified + * process. Use the regular "ptrace_may_access()" checks. + */ + if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) { + rcu_read_unlock(); + err = -EPERM; + goto out_put; + } + rcu_read_unlock(); + } + + err = sched_core_share_tasks(current, task); +out_put: + if (task) + put_task_struct(task); + return err; +} + /* CGroup interface */ static u64 cpu_core_tag_read_u64(struct cgroup_subsys_state *css, struct cftype *cft) { diff --git a/kernel/sys.c b/kernel/sys.c index 6401880dff74..17911b8680b1 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2530,6 +2530,9 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, error = (current->flags & PR_IO_FLUSHER) == PR_IO_FLUSHER; break; + case PR_SCHED_CORE_SHARE: + error = sched_core_share_pid(arg2); + break; default: error = -EINVAL; break; diff --git a/tools/include/uapi/linux/prctl.h b/tools/include/uapi/linux/prctl.h index 07b4f8131e36..9318f643c4b3 100644 --- a/tools/include/uapi/linux/prctl.h +++ b/tools/include/uapi/linux/prctl.h @@ -238,4 +238,7 @@ struct prctl_mm_map { #define PR_SET_IO_FLUSHER 57 #define PR_GET_IO_FLUSHER 58 +/* Request the scheduler to share a core */ +#define PR_SCHED_CORE_SHARE 59 + #endif /* _LINUX_PRCTL_H */ -- 2.29.0.rc1.297.gfa9743e501-goog