From nobody Tue Jun 30 06:32:17 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 99261C433F5 for ; Mon, 24 Jan 2022 10:53:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237042AbiAXKxK (ORCPT ); Mon, 24 Jan 2022 05:53:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236911AbiAXKxH (ORCPT ); Mon, 24 Jan 2022 05:53:07 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD270C06173B for ; Mon, 24 Jan 2022 02:53:06 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 634E3B80EF3 for ; Mon, 24 Jan 2022 10:53:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 99E6CC340EA; Mon, 24 Jan 2022 10:52:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643021584; bh=rjYcfivnXLMYloSB+GYd4XhVGaJkd4qwOtzVclJx7YQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WE3WAmX+KG/sxElRQR+9R3/bW0UbN+8WVD0kpOWh5bt4G9Hflz7r3d4LI94uRaFLz VG/fIrZZNCK6FyGyMITAwSFkHwGdZDiKv4GNksHnrRg8Kf8/g1a52eROBcsVdT4AyJ DMQkbk788X1Tl2Lccn/MssA10QE3mVf9RIFNWNG5mbqlbsunaZ4LyIoxJnCyXyzsnR m8U+9NstvgwxdYC3QOgwcsCOp1N2QcX2PDbLTmvrcj1htnSa8fMmcvp6qfOE2BOG8z vQ5/iwYPxuYKZgztk/6YT6H4h69M7669Q7eZw46RQeF3mJj2+85L4mY0bOloa9AK+4 u+3h1iVxm1mVw== From: Christian Brauner To: Joel Fernandes , Chris Hyser , Daniel Bristot de Oliveira , Peter Zijlstra , linux-kernel@vger.kernel.org Cc: Peter Collingbourne , Dietmar Eggemann , Thomas Gleixner , Mel Gorman , Vincent Guittot , Juri Lelli , Catalin Marinas , Ingo Molnar , Steven Rostedt , Ben Segall , Sebastian Andrzej Siewior , Balbir Singh , Christian Brauner , Jens Axboe , Tejun Heo Subject: [resend RFC 1/3] pid: introduce task_by_pid() Date: Mon, 24 Jan 2022 11:52:45 +0100 Message-Id: <20220124105247.2118990-2-brauner@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220124105247.2118990-1-brauner@kernel.org> References: <20220124105247.2118990-1-brauner@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=11875; h=from:subject; bh=rjYcfivnXLMYloSB+GYd4XhVGaJkd4qwOtzVclJx7YQ=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMSS+a/mz/k3jp7+iFzlrLj5iOcH7PEDQO+VY+cO0HSw61Y35 D6pdO0pZGMS4GGTFFFkc2k3C5ZbzVGw2ytSAmcPKBDKEgYtTACbSrMHwv6JhYbKr+MM5/Nd94s7kdY fONt2W9UtD+/PK2VH8e561f2D4K90WvSc/ONds1fkZ/7IftCg0mIhcY5exPJ+yzyphi0Q9OwA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" We have a lot of places that open code if (who) p =3D find_task_by_vpid(who); else p =3D current; Introduce a simpler helper which can be used instead. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Jens Axboe Cc: Peter Zijlstra Cc: Juri Lelli Cc: Vincent Guittot Cc: Steven Rostedt Cc: Daniel Bristot de Oliveira Cc: Tejun Heo Cc: Sebastian Andrzej Siewior Cc: linux-kernel@vger.kernel.org Signed-off-by: Christian Brauner --- arch/mips/kernel/mips-mt-fpaff.c | 14 ++----------- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 19 +++++++----------- block/ioprio.c | 10 ++-------- include/linux/sched.h | 7 +++++++ kernel/cgroup/cgroup.c | 12 ++++-------- kernel/events/core.c | 5 +---- kernel/futex/syscalls.c | 20 ++++++------------- kernel/pid.c | 5 +++++ kernel/sched/core.c | 27 ++++++++------------------ kernel/sched/core_sched.c | 12 ++++-------- kernel/sys.c | 12 +++--------- mm/mempolicy.c | 2 +- 12 files changed, 50 insertions(+), 95 deletions(-) diff --git a/arch/mips/kernel/mips-mt-fpaff.c b/arch/mips/kernel/mips-mt-fp= aff.c index 67e130d3f038..53c8a56815ea 100644 --- a/arch/mips/kernel/mips-mt-fpaff.c +++ b/arch/mips/kernel/mips-mt-fpaff.c @@ -33,16 +33,6 @@ unsigned long mt_fpemul_threshold; * updated when kernel/sched/core.c changes. */ =20 -/* - * find_process_by_pid - find a process with a matching PID value. - * used in sys_sched_set/getaffinity() in kernel/sched/core.c, so - * cloned here. - */ -static inline struct task_struct *find_process_by_pid(pid_t pid) -{ - return pid ? find_task_by_vpid(pid) : current; -} - /* * check the target process has a UID that matches the current process's */ @@ -79,7 +69,7 @@ asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, u= nsigned int len, cpus_read_lock(); rcu_read_lock(); =20 - p =3D find_process_by_pid(pid); + p =3D task_by_pid(pid); if (!p) { rcu_read_unlock(); cpus_read_unlock(); @@ -170,7 +160,7 @@ asmlinkage long mipsmt_sys_sched_getaffinity(pid_t pid,= unsigned int len, rcu_read_lock(); =20 retval =3D -ESRCH; - p =3D find_process_by_pid(pid); + p =3D task_by_pid(pid); if (!p) goto out_unlock; retval =3D security_task_getscheduler(p); diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/r= esctrl/rdtgroup.c index b57b3db9a6a7..577d0ffebb9d 100644 --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c @@ -660,19 +660,14 @@ static int rdtgroup_move_task(pid_t pid, struct rdtgr= oup *rdtgrp, int ret; =20 rcu_read_lock(); - if (pid) { - tsk =3D find_task_by_vpid(pid); - if (!tsk) { - rcu_read_unlock(); - rdt_last_cmd_printf("No task %d\n", pid); - return -ESRCH; - } - } else { - tsk =3D current; - } - - get_task_struct(tsk); + tsk =3D task_by_pid(pid); + if (tsk) + get_task_struct(tsk); rcu_read_unlock(); + if (!tsk) { + rdt_last_cmd_printf("No task %d\n", pid); + return -ESRCH; + } =20 ret =3D rdtgroup_task_write_permission(tsk, of); if (!ret) diff --git a/block/ioprio.c b/block/ioprio.c index 2fe068fcaad5..934e96cd495b 100644 --- a/block/ioprio.c +++ b/block/ioprio.c @@ -81,10 +81,7 @@ SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, i= oprio) rcu_read_lock(); switch (which) { case IOPRIO_WHO_PROCESS: - if (!who) - p =3D current; - else - p =3D find_task_by_vpid(who); + p =3D task_by_pid(who); if (p) ret =3D set_task_ioprio(p, ioprio); break; @@ -176,10 +173,7 @@ SYSCALL_DEFINE2(ioprio_get, int, which, int, who) rcu_read_lock(); switch (which) { case IOPRIO_WHO_PROCESS: - if (!who) - p =3D current; - else - p =3D find_task_by_vpid(who); + p =3D task_by_pid(who); if (p) ret =3D get_task_ioprio(p); break; diff --git a/include/linux/sched.h b/include/linux/sched.h index 508b91d57470..0408372594dd 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1912,6 +1912,13 @@ extern unsigned long init_stack[THREAD_SIZE / sizeof= (unsigned long)]; */ =20 extern struct task_struct *find_task_by_vpid(pid_t nr); +/** + * task_by_pid - find a process with a matching PID value. + * @pid: the pid in question. + * + * The task of @pid, if found. %NULL otherwise. + */ +extern struct task_struct *task_by_pid(pid_t nr); extern struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namesp= ace *ns); =20 /* diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index b31e1465868a..3fddd5003a2b 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -2839,14 +2839,10 @@ struct task_struct *cgroup_procs_write_start(char *= buf, bool threadgroup, } =20 rcu_read_lock(); - if (pid) { - tsk =3D find_task_by_vpid(pid); - if (!tsk) { - tsk =3D ERR_PTR(-ESRCH); - goto out_unlock_threadgroup; - } - } else { - tsk =3D current; + tsk =3D task_by_pid(pid); + if (!tsk) { + tsk =3D ERR_PTR(-ESRCH); + goto out_unlock_threadgroup; } =20 if (threadgroup) diff --git a/kernel/events/core.c b/kernel/events/core.c index fc18664f49b0..9f9ea469f1d1 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -4608,10 +4608,7 @@ find_lively_task_by_vpid(pid_t vpid) struct task_struct *task; =20 rcu_read_lock(); - if (!vpid) - task =3D current; - else - task =3D find_task_by_vpid(vpid); + task =3D task_by_pid(vpid); if (task) get_task_struct(task); rcu_read_unlock(); diff --git a/kernel/futex/syscalls.c b/kernel/futex/syscalls.c index 086a22d1adb7..76b5c5389214 100644 --- a/kernel/futex/syscalls.c +++ b/kernel/futex/syscalls.c @@ -57,13 +57,9 @@ SYSCALL_DEFINE3(get_robust_list, int, pid, rcu_read_lock(); =20 ret =3D -ESRCH; - if (!pid) - p =3D current; - else { - p =3D find_task_by_vpid(pid); - if (!p) - goto err_unlock; - } + p =3D task_by_pid(pid); + if (!p) + goto err_unlock; =20 ret =3D -EPERM; if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS)) @@ -326,13 +322,9 @@ COMPAT_SYSCALL_DEFINE3(get_robust_list, int, pid, rcu_read_lock(); =20 ret =3D -ESRCH; - if (!pid) - p =3D current; - else { - p =3D find_task_by_vpid(pid); - if (!p) - goto err_unlock; - } + p =3D task_by_pid(pid); + if (!p) + goto err_unlock; =20 ret =3D -EPERM; if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS)) diff --git a/kernel/pid.c b/kernel/pid.c index 2fc0a16ec77b..1cd82fa58273 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -422,6 +422,11 @@ struct task_struct *find_task_by_vpid(pid_t vnr) return find_task_by_pid_ns(vnr, task_active_pid_ns(current)); } =20 +struct task_struct *task_by_pid(pid_t nr) +{ + return nr ? find_task_by_vpid(nr) : current; +} + struct task_struct *find_get_task_by_vpid(pid_t nr) { struct task_struct *task; diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 2e4ae00e52d1..196543f0c39a 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -7174,17 +7174,6 @@ unsigned long sched_cpu_util(int cpu, unsigned long = max) } #endif /* CONFIG_SMP */ =20 -/** - * find_process_by_pid - find a process with a matching PID value. - * @pid: the pid in question. - * - * The task of @pid, if found. %NULL otherwise. - */ -static struct task_struct *find_process_by_pid(pid_t pid) -{ - return pid ? find_task_by_vpid(pid) : current; -} - /* * sched_setparam() passes in -1 for its policy, to let the functions * it calls know not to change it. @@ -7627,7 +7616,7 @@ do_sched_setscheduler(pid_t pid, int policy, struct s= ched_param __user *param) =20 rcu_read_lock(); retval =3D -ESRCH; - p =3D find_process_by_pid(pid); + p =3D task_by_pid(pid); if (likely(p)) get_task_struct(p); rcu_read_unlock(); @@ -7750,7 +7739,7 @@ SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sch= ed_attr __user *, uattr, =20 rcu_read_lock(); retval =3D -ESRCH; - p =3D find_process_by_pid(pid); + p =3D task_by_pid(pid); if (likely(p)) get_task_struct(p); rcu_read_unlock(); @@ -7782,7 +7771,7 @@ SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid) =20 retval =3D -ESRCH; rcu_read_lock(); - p =3D find_process_by_pid(pid); + p =3D task_by_pid(pid); if (p) { retval =3D security_task_getscheduler(p); if (!retval) @@ -7811,7 +7800,7 @@ SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sc= hed_param __user *, param) return -EINVAL; =20 rcu_read_lock(); - p =3D find_process_by_pid(pid); + p =3D task_by_pid(pid); retval =3D -ESRCH; if (!p) goto out_unlock; @@ -7894,7 +7883,7 @@ SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sch= ed_attr __user *, uattr, return -EINVAL; =20 rcu_read_lock(); - p =3D find_process_by_pid(pid); + p =3D task_by_pid(pid); retval =3D -ESRCH; if (!p) goto out_unlock; @@ -8003,7 +7992,7 @@ long sched_setaffinity(pid_t pid, const struct cpumas= k *in_mask) =20 rcu_read_lock(); =20 - p =3D find_process_by_pid(pid); + p =3D task_by_pid(pid); if (!p) { rcu_read_unlock(); return -ESRCH; @@ -8082,7 +8071,7 @@ long sched_getaffinity(pid_t pid, struct cpumask *mas= k) rcu_read_lock(); =20 retval =3D -ESRCH; - p =3D find_process_by_pid(pid); + p =3D task_by_pid(pid); if (!p) goto out_unlock; =20 @@ -8482,7 +8471,7 @@ static int sched_rr_get_interval(pid_t pid, struct ti= mespec64 *t) =20 retval =3D -ESRCH; rcu_read_lock(); - p =3D find_process_by_pid(pid); + p =3D task_by_pid(pid); if (!p) goto out_unlock; =20 diff --git a/kernel/sched/core_sched.c b/kernel/sched/core_sched.c index 1fb45672ec85..0c40445337c5 100644 --- a/kernel/sched/core_sched.c +++ b/kernel/sched/core_sched.c @@ -148,14 +148,10 @@ int sched_core_share_pid(unsigned int cmd, pid_t pid,= enum pid_type type, return -EINVAL; =20 rcu_read_lock(); - if (pid =3D=3D 0) { - task =3D current; - } else { - task =3D find_task_by_vpid(pid); - if (!task) { - rcu_read_unlock(); - return -ESRCH; - } + task =3D task_by_pid(pid); + if (!task) { + rcu_read_unlock(); + return -ESRCH; } get_task_struct(task); rcu_read_unlock(); diff --git a/kernel/sys.c b/kernel/sys.c index ecc4cf019242..9460e2eefaad 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -222,10 +222,7 @@ SYSCALL_DEFINE3(setpriority, int, which, int, who, int= , niceval) rcu_read_lock(); switch (which) { case PRIO_PROCESS: - if (who) - p =3D find_task_by_vpid(who); - else - p =3D current; + p =3D task_by_pid(who); if (p) error =3D set_one_prio(p, niceval, error); break; @@ -285,10 +282,7 @@ SYSCALL_DEFINE2(getpriority, int, which, int, who) rcu_read_lock(); switch (which) { case PRIO_PROCESS: - if (who) - p =3D find_task_by_vpid(who); - else - p =3D current; + p =3D task_by_pid(who); if (p) { niceval =3D nice_to_rlimit(task_nice(p)); if (niceval > retval) @@ -1659,7 +1653,7 @@ SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, = resource, } =20 rcu_read_lock(); - tsk =3D pid ? find_task_by_vpid(pid) : current; + tsk =3D task_by_pid(pid); if (!tsk) { rcu_read_unlock(); return -ESRCH; diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 028e8dd82b44..c113e274204a 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -1613,7 +1613,7 @@ static int kernel_migrate_pages(pid_t pid, unsigned l= ong maxnode, =20 /* Find the mm_struct */ rcu_read_lock(); - task =3D pid ? find_task_by_vpid(pid) : current; + task =3D task_by_pid(pid); if (!task) { rcu_read_unlock(); err =3D -ESRCH; --=20 2.32.0 From nobody Tue Jun 30 06:32:17 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B42F7C433EF for ; Mon, 24 Jan 2022 10:53:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236948AbiAXKxO (ORCPT ); Mon, 24 Jan 2022 05:53:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237041AbiAXKxK (ORCPT ); Mon, 24 Jan 2022 05:53:10 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C566FC061401 for ; Mon, 24 Jan 2022 02:53:09 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 540DC612F2 for ; Mon, 24 Jan 2022 10:53:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AD27EC340E9; Mon, 24 Jan 2022 10:53:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643021588; bh=0IJRbMvxIVHHKgHnDPeMAzag7U1Dn6cvWSryKx8Upv4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=acfqtPz+zBjNDnVpyyoMsdlXbyztoTH9bmR10JfSI+5poMudCH6tyXdZSavN96FBl 3/RU9qsnXyw8uK2FZ4zsNaYcaRACC2lv9bFbsHIE/cbZxCJJhAM+wmlQplwU3VGMag NNg+SpDP77Rh0DbVjYcIPkFJzJzKF6yTGMf1dbMS1Qp9dOwgEK3haPzbZEh/C9sJ+6 +XAwVio+s1RrbXSDQamm/MDelol/RDOhXEe7HYuQRbV5ED5dJGlHuw6plsK0xIACkk yah272oYrcohmiGv06G95zXoOsqd5k8kOaxUjbtQnze+YjBqVgWhuNFjsaWuUIxIYk VRBhz9WeR+u/Q== From: Christian Brauner To: Joel Fernandes , Chris Hyser , Daniel Bristot de Oliveira , Peter Zijlstra , linux-kernel@vger.kernel.org Cc: Peter Collingbourne , Dietmar Eggemann , Thomas Gleixner , Mel Gorman , Vincent Guittot , Juri Lelli , Catalin Marinas , Ingo Molnar , Steven Rostedt , Ben Segall , Sebastian Andrzej Siewior , Balbir Singh , Christian Brauner Subject: [resend RFC 2/3] sched/prctl: add PR_SCHED_CORE_SHARE command Date: Mon, 24 Jan 2022 11:52:46 +0100 Message-Id: <20220124105247.2118990-3-brauner@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220124105247.2118990-1-brauner@kernel.org> References: <20220124105247.2118990-1-brauner@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" X-Developer-Signature: v=1; a=openpgp-sha256; l=12803; h=from:subject; bh=0IJRbMvxIVHHKgHnDPeMAzag7U1Dn6cvWSryKx8Upv4=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMSS+a/kTseN+ksDvhw+6s1ruCx7bKs2xMFXoLePRxY+aH/A/ P+o0saOUhUGMi0FWTJHFod0kXG45T8Vmo0wNmDmsTCBDGLg4BWAiogcYGXaavXHuavC8sqUkM3x60o vLd2Q/h7S9n31GqFbLfK/K4l+MDK+u3N8er3D39VfNHx+XhfhzyW72Ky2Kk3/iKHJKa/YTAwYA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This adds the new PR_CORE_SCHED prctl() command PR_SCHED_CORE_SHARE to allow a third process to pull a core scheduling domain from one task and push it to another task. The core scheduling uapi is exposed via the PR_SCHED_CORE option of the prctl() system call. Two commands can be used to alter the core scheduling domain of a task: 1. PR_SCHED_CORE_SHARE_TO This command takes the cookie for the caller's core scheduling domain and applies it to a target task identified by passing a pid. 2. PR_SCHED_CORE_SHARE_FROM This command takes the cookie for a task's core scheduling domain and applies it to the calling task. While these options cover nearly all use-cases they are rather inconvient for some common use-cases. A vm/container manager often supervises a large number of vms/containers: vm/container manager vm-supervisor-1 container-supervisor-1 vm-supervisor-2 container-s= upervisor-2 Where none of the vms/container are its immediate children. For container managers each container often has a separate supervising process and the workload is the parent of the container. In the example below the supervising process is "[lxc monitor]" and the workload is "/sbin/init" and all descendant processes: =E2=94=9C=E2=94=80[lxc monitor] /var/lib/lxd/containers imp1 =E2=94=82 =E2=94=94=E2=94=80systemd =E2=94=82 =E2=94=9C=E2=94=80agetty -o -p -- \\u --noclear --keep-baud= console 115200,38400,9600 linux =E2=94=82 =E2=94=9C=E2=94=80cron -f -P =E2=94=82 =E2=94=9C=E2=94=80dbus-daemon --system --address=3Dsystemd:= --nofork --nopidfile --systemd-activation --syslog-only =E2=94=82 =E2=94=9C=E2=94=80networkd-dispat /usr/bin/networkd-dispatc= her --run-startup-triggers =E2=94=82 =E2=94=9C=E2=94=80rsyslogd -n -iNONE =E2=94=82 =E2=94=82 =E2=94=9C=E2=94=80{rsyslogd} =E2=94=82 =E2=94=82 =E2=94=94=E2=94=80{rsyslogd} =E2=94=82 =E2=94=9C=E2=94=80systemd-journal =E2=94=82 =E2=94=9C=E2=94=80systemd-logind =E2=94=82 =E2=94=9C=E2=94=80systemd-network =E2=94=82 =E2=94=9C=E2=94=80systemd-resolve =E2=94=82 =E2=94=94=E2=94=80systemd-udevd Similiar in spirit but different in layout a vm often has a supervising process and multiple threads for each vcpu: =E2=94=9C=E2=94=80qemu-system-x86 -S -name f2-vm [...] =E2=94=82 =E2=94=9C=E2=94=80{qemu-system-x86} =E2=94=82 =E2=94=9C=E2=94=80{qemu-system-x86} =E2=94=82 =E2=94=9C=E2=94=80{qemu-system-x86} =E2=94=82 =E2=94=9C=E2=94=80{qemu-system-x86} =E2=94=82 =E2=94=9C=E2=94=80{qemu-system-x86} =E2=94=82 =E2=94=9C=E2=94=80{qemu-system-x86} =E2=94=82 =E2=94=9C=E2=94=80{qemu-system-x86} =E2=94=82 =E2=94=9C=E2=94=80{qemu-system-x86} =E2=94=82 =E2=94=9C=E2=94=80{qemu-system-x86} =E2=94=82 =E2=94=9C=E2=94=80{qemu-system-x86} =E2=94=82 =E2=94=94=E2=94=80{qemu-system-x86} So ultimately an approximation of that layout would be: vm/container manager vm-supervisor-1 container-supervisor-1 vm-supervisor-2 container-s= upervisor-2 | | | | vcpus workload vcpus work= load (/sbin/init) (/sbin= /init) For containers a new core scheduling domain is allocated for the init process. Any descendant processes and threads init spawns will automatically inherit the correct core scheduling domain. For vms a new core scheduling domain is allocated and each vcpu thread will be made to join the new core scheduling domain. Whenever the tool or library that we use to run containers or vms exposes an option to automatically create a new core scheduling domain we will make use of it. However that is not always the case. In such cases the vm/container manager will need to allocate and set the core scheduling domain for the relevant processes or threads. Neither the vm/container mananger nor the indivial vm/container supervisors are supposed to run in any or the same core scheduling domain as the respective vcpus/workloads. So in order to create to create a new core scheduling domain we need to fork() off a new helper process which allocates a core scheduling domain and then pushes the cookie for the core scheduling domain to the relevant vcpus/workloads. This works but things get rather tricky, especially for containers, when a new process is supposed to be spawned into a running container. An important step in creating a new process inside a running container involves: - getting a handle on the container's init process (pid or nowadays often a pidfd) - getting a handle on the container's namespaces (namespace file descriptors reachable via /proc//ns/ or nowadays often a pidfd) - calling setns() either on each namespace file descriptor individually or on the pidfd of the init process An important sub-step here is to attach to the container's pid namespace via setns(). After attaching to the container's pid namespace any process created via a fork()-like system calls will be a full member of the container's pid namespace. So attaching often involves two child processes. The first child simply attaches to the namespaces of the container including the container's pid namespace. The second child fork()s and ultimately exec()s thereby guaranteeing that the newly created process is a full member of the container's pid namespace: first_child =3D fork(); if (first_child =3D=3D 0) { setns(CLONE_NEWPID); second_child =3D fork(); if (second_child =3D=3D 0) { execlp(); } } As part of this we also need to make sure that the second child - the one ultimately exec()ing the relevant programm in an already running container - joins the core scheduling domain of the container. When the container runs in a new pid namespace this can usually be done by calling: first_child =3D fork(); if (first_child =3D=3D 0) { setns(CLONE_NEWPID); second_child =3D fork(); if (second_child =3D=3D 0) { prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_FROM, 1, PR_SCHED_CORE_SCOPE_THREAD, 0); execlp(); } } from the second child since we know that pid 1 in a container running inside of a separate pid namespace is the correct process to get the core scheduling domain from. However, this doesn't work when the container does not run in a separate pid namespace or when it shares the pid namespace with another container. In these scenarios we can't simply call PR_SCHED_CORE_SHARE_FROM from the second child since we don't know the correct pid number to call it on in the pid namespace. (Note it is of course possible to learn the pid of the process in the relevant pid namespace but it is rather complex involving three separate processes and an AF_UNIX domain socket over which to send a message including struct ucred from which to learn the relevant pid. But that doesn't work in all cases since it requires privileges to translate arbitrary pids. In any case, this is not an option for performance reasons alone. However, I do also have a separate patchset in [1] allowing translation of pids between pid namespaces which will help with that in the future - something which I had discussed with Joel a while back but haven't pushed for yet since implementing it early 2020. Both patches are useful independent of one another.) Additionally, we ideally always want to manage the core scheduling domain from the first child since the first child knows the pids for the relevant processes in its current pid namespace. The first child knows the pid of the init process in the current pid namespace from which to pull the core scheduling domain and it knows the pid of the second child it created to which to apply the core scheduling domain. The core scheduling domain of the first child needs to be unaffected as it might run sensitive codepaths that should not be exposed in smt attacks. The new PR_CORE_SCHED_SHARE command for the PR_SCHED_CORE prctl() option allows to support this and other use-cases by making it possible to pull the core scheduling domain from a task identified via its pid and push it to another task identified via its pid from a third managing task: prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE, , PR_SCHED_CORE_SCOPE_{THREAD,THREAD_GROUP,PROCESS_GROUP}, ) In order to use PR_SCHED_CORE_SHARE the caller must have ptrace_may_access() rights to both the task from which to take the core scheduling domain and to the task to which to apply the core scheduling domain. If the caller passes zero as the 5th argument then its own core scheduling domain is applied to the target making the option adhere to regular prctl() semantics. [1]: https://git.kernel.org/brauner/h/ioctl_ns_get_init_pid https://git.kernel.org/brauner/c/1ad81fd698dd7e6511c3db422eba42dec3ce1= b08 Cc: Peter Collingbourne Cc: Dietmar Eggemann Cc: Joel Fernandes Cc: Thomas Gleixner Cc: Mel Gorman Cc: Vincent Guittot Cc: Chris Hyser Cc: Juri Lelli Cc: Catalin Marinas Cc: Ingo Molnar Cc: Daniel Bristot de Oliveira Cc: Steven Rostedt Cc: Ben Segall Cc: Balbir Singh Cc: Peter Zijlstra Cc: linux-kernel@vger.kernel.org Signed-off-by: Christian Brauner --- include/linux/sched.h | 2 +- include/uapi/linux/prctl.h | 3 ++- kernel/sched/core_sched.c | 32 +++++++++++++++++++++++++++++--- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 0408372594dd..2eeac7a341ad 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -2341,7 +2341,7 @@ const struct cpumask *sched_trace_rd_span(struct root= _domain *rd); extern void sched_core_free(struct task_struct *tsk); extern void sched_core_fork(struct task_struct *p); extern int sched_core_share_pid(unsigned int cmd, pid_t pid, enum pid_type= type, - unsigned long uaddr); + unsigned long arg); #else static inline void sched_core_free(struct task_struct *tsk) { } static inline void sched_core_fork(struct task_struct *p) { } diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index e998764f0262..e53945dadede 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -267,7 +267,8 @@ struct prctl_mm_map { # define PR_SCHED_CORE_CREATE 1 /* create unique core_sched cookie */ # define PR_SCHED_CORE_SHARE_TO 2 /* push core_sched cookie to pid */ # define PR_SCHED_CORE_SHARE_FROM 3 /* pull core_sched cookie to pid */ -# define PR_SCHED_CORE_MAX 4 +# define PR_SCHED_CORE_SHARE 4 +# define PR_SCHED_CORE_MAX 5 # define PR_SCHED_CORE_SCOPE_THREAD 0 # define PR_SCHED_CORE_SCOPE_THREAD_GROUP 1 # define PR_SCHED_CORE_SCOPE_PROCESS_GROUP 2 diff --git a/kernel/sched/core_sched.c b/kernel/sched/core_sched.c index 0c40445337c5..241bb38f5e55 100644 --- a/kernel/sched/core_sched.c +++ b/kernel/sched/core_sched.c @@ -129,9 +129,10 @@ static void __sched_core_set(struct task_struct *p, un= signed long cookie) =20 /* Called from prctl interface: PR_SCHED_CORE */ int sched_core_share_pid(unsigned int cmd, pid_t pid, enum pid_type type, - unsigned long uaddr) + unsigned long arg) { - unsigned long cookie =3D 0, id =3D 0; + unsigned long cookie =3D 0, id =3D 0, uaddr =3D 0; + pid_t pid_share =3D -1; struct task_struct *task, *p; struct pid *grp; int err =3D 0; @@ -144,9 +145,20 @@ int sched_core_share_pid(unsigned int cmd, pid_t pid, = enum pid_type type, BUILD_BUG_ON(PR_SCHED_CORE_SCOPE_PROCESS_GROUP !=3D PIDTYPE_PGID); =20 if (type > PIDTYPE_PGID || cmd >=3D PR_SCHED_CORE_MAX || pid < 0 || - (cmd !=3D PR_SCHED_CORE_GET && uaddr)) + (cmd !=3D PR_SCHED_CORE_GET && cmd !=3D PR_SCHED_CORE_SHARE && arg)) return -EINVAL; =20 + switch (cmd) { + case PR_SCHED_CORE_GET: + uaddr =3D arg; + break; + case PR_SCHED_CORE_SHARE: + pid_share =3D arg; + if (pid_share < 0) + return -EINVAL; + break; + } + rcu_read_lock(); task =3D task_by_pid(pid); if (!task) { @@ -200,6 +212,20 @@ int sched_core_share_pid(unsigned int cmd, pid_t pid, = enum pid_type type, __sched_core_set(current, cookie); goto out; =20 + case PR_SCHED_CORE_SHARE: + rcu_read_lock(); + p =3D task_by_pid(pid_share); + if (!p) + err =3D -ESRCH; + else if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS)) + err =3D -EPERM; + if (!err) + cookie =3D sched_core_clone_cookie(p); + rcu_read_unlock(); + if (err) + goto out; + break; + default: err =3D -EINVAL; goto out; --=20 2.32.0 From nobody Tue Jun 30 06:32:17 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 79DDAC433EF for ; Mon, 24 Jan 2022 10:53:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237130AbiAXKxU (ORCPT ); Mon, 24 Jan 2022 05:53:20 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:34414 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237038AbiAXKxO (ORCPT ); Mon, 24 Jan 2022 05:53:14 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4EAE7612F2; Mon, 24 Jan 2022 10:53:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4D8F0C340E4; Mon, 24 Jan 2022 10:53:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643021593; bh=ZMfQWUpk0eDWkXdZTigFyfC36ehh99jynZEFFZLSbok=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rMkT1h22frBQn/cTz/3is31icPq12OL/glW8xho0ut6HBfsVoHBMko82K9bGo0+tx tlcfD/x+m4USH16VRHx5S69dRXF/ei/i1WVuyH/FXulF1IblTar3w0LW98KcJr2ceg u3kT++IizLHxgYS/Y5aY6e04RZyk/mxL/8SPzgQXSth5dvf8PN0AkSi2e8o049RC5j i/Uhz6iH6mZVZ7tCVi1Y1TeSOuhVkhg4shN+avG+sWPtSJ4uKyDIo75HaSZ+Po7hgE FSpy69yHtrJ9AcLKsWBuFPsHefIiKTOLMjCdauaIoKqCydijPSIwqXH+8d/5y+JZBG 7Pf1QIYrwHkmg== From: Christian Brauner To: Joel Fernandes , Chris Hyser , Daniel Bristot de Oliveira , Peter Zijlstra , linux-kernel@vger.kernel.org Cc: Peter Collingbourne , Dietmar Eggemann , Thomas Gleixner , Mel Gorman , Vincent Guittot , Juri Lelli , Catalin Marinas , Ingo Molnar , Steven Rostedt , Ben Segall , Sebastian Andrzej Siewior , Balbir Singh , Christian Brauner , Shuah Khan , linux-kselftest@vger.kernel.org Subject: [resend RFC 3/3] tests: add new PR_SCHED_CORE_SHARE test Date: Mon, 24 Jan 2022 11:52:47 +0100 Message-Id: <20220124105247.2118990-4-brauner@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220124105247.2118990-1-brauner@kernel.org> References: <20220124105247.2118990-1-brauner@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3036; h=from:subject; bh=ZMfQWUpk0eDWkXdZTigFyfC36ehh99jynZEFFZLSbok=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMSS+a/nrsHpjTQmnzVYRq6/3Eg/6iSclPb25elvHJpOypz8m LJ4Y2VHKwiDGxSArpsji0G4SLrecp2KzUaYGzBxWJpAhDFycAjCRE+0M/+N11D70tG7ccyFuwg5W3T fvIj5PW/L9YlHNxZ/vHospeeUz/I+T/bDRPidh4TT3txMrMmvab/unreA+5m1tLpP9viVRlxUA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add tests for the new PR_SCHED_CORE_SHARE command. Cc: Peter Collingbourne Cc: Dietmar Eggemann Cc: Joel Fernandes Cc: Thomas Gleixner Cc: Mel Gorman Cc: Vincent Guittot Cc: Chris Hyser Cc: Shuah Khan Cc: Juri Lelli Cc: Catalin Marinas Cc: Ingo Molnar Cc: Daniel Bristot de Oliveira Cc: Steven Rostedt Cc: Ben Segall Cc: Balbir Singh Cc: Peter Zijlstra Cc: linux-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: linux-kselftest@vger.kernel.org Signed-off-by: Christian Brauner --- tools/testing/selftests/sched/cs_prctl_test.c | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tools/testing/selftests/sched/cs_prctl_test.c b/tools/testing/= selftests/sched/cs_prctl_test.c index 8109b17dc764..985b83fe7221 100644 --- a/tools/testing/selftests/sched/cs_prctl_test.c +++ b/tools/testing/selftests/sched/cs_prctl_test.c @@ -229,6 +229,7 @@ int main(int argc, char *argv[]) int pidx; int pid; int opt; + int i; =20 while ((opt =3D getopt(argc, argv, ":hkT:P:d:")) !=3D -1) { switch (opt) { @@ -325,6 +326,28 @@ int main(int argc, char *argv[]) validate(get_cs_cookie(pid) !=3D 0); validate(get_cs_cookie(pid) =3D=3D get_cs_cookie(procs[pidx].thr_tids[0])= ); =20 + printf("\n## Set a new cookie on a single thread/PR_SCHED_CORE_SCOPE_THRE= AD [%d]\n", pid); + if (_prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, pid, PR_SCHED_CORE_SCOPE_= THREAD, 0) < 0) + handle_error("core_sched create failed -- PR_SCHED_CORE_SCOPE_THREAD"); + disp_processes(num_processes, procs); + + validate(get_cs_cookie(pid) !=3D get_cs_cookie(procs[pidx].thr_tids[0])); + + printf("\n## Copy cookie from a thread [%d] to [%d] as PR_SCHED_CORE_SCOP= E_THREAD\n", pid, procs[pidx].thr_tids[0]); + if (_prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE, procs[pidx].thr_tids[0], P= R_SCHED_CORE_SCOPE_THREAD, pid) < 0) + handle_error("core_sched share cookie from and to thread failed -- PR_SC= HED_CORE_SCOPE_THREAD"); + disp_processes(num_processes, procs); + + validate(get_cs_cookie(pid) =3D=3D get_cs_cookie(procs[pidx].thr_tids[0])= ); + + printf("\n## Copy cookie from a thread [%d] to [%d] as PR_SCHED_CORE_SCOP= E_THREAD_GROUP\n", pid, pid); + if (_prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE, pid, PR_SCHED_CORE_SCOPE_T= HREAD_GROUP, pid) < 0) + handle_error("core_sched share cookie from and to thread-group failed --= PR_SCHED_CORE_SCOPE_THREAD_GROUP"); + disp_processes(num_processes, procs); + + for (i =3D 0; i < procs[pidx].num_threads; ++i) + validate(get_cs_cookie(pid) =3D=3D get_cs_cookie(procs[pidx].thr_tids[i]= )); + if (errors) { printf("TESTS FAILED. errors: %d\n", errors); res =3D 10; --=20 2.32.0