From nobody Fri Dec 19 16:05:40 2025 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 743ADEDEC50 for ; Wed, 13 Sep 2023 13:11:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240822AbjIMNLh (ORCPT ); Wed, 13 Sep 2023 09:11:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35138 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240809AbjIMNLT (ORCPT ); Wed, 13 Sep 2023 09:11:19 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A530619B4; Wed, 13 Sep 2023 06:11:15 -0700 (PDT) Date: Wed, 13 Sep 2023 13:11:13 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694610674; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=DofI4X0SwM4cFKXg33+5VCuGohNRvsuXKMzK44gzZPw=; b=BR9MjLkliLA6G0LaMQm9kY1ecIaR6emAees6PMbnKWPmdcx6vxuKen9oph4if/BOrK0jRk aIXH+PUi5BK5d8lIItJAcAEDslvC23K93kbVNyZDhKqznOXAfWg9qwTTfebHMtBLTMx4h2 +Tu1Ncbe9xg2QMokSYtVyR5aaGM61mvfMfneA7B+sVgsu5v42yxjvJKI8ZtmvAxtXdvXnG i71xrNrHbaoyF+nVDuINmruikmF4tPhweLcI2uOEaCTcSKKs++TmioUpzkNeL2Og3k2wu+ ZLHpqQjh3uTyI+YYaygxFqx42+JmnEMq1v7BTfJ6J4TeCobO9ChGuaVNGL2oMg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694610674; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=DofI4X0SwM4cFKXg33+5VCuGohNRvsuXKMzK44gzZPw=; b=60Z3NBBw591pkqBJLg1YDhdkexfPkT6DkpO8mXScNmmgMIO+KGjGYrLL7eokbuC2kU0ZgP smfEwknQSVk02mDg== From: "tip-bot2 for Peter Zijlstra" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: sched/core] sched: Simplify syscalls Cc: "Peter Zijlstra (Intel)" , Ingo Molnar , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <169461067369.27769.2835016945011485874.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the sched/core branch of tip: Commit-ID: febe162d4d9158cf2b5d48fdd440db7bb55dd622 Gitweb: https://git.kernel.org/tip/febe162d4d9158cf2b5d48fdd440db7bb= 55dd622 Author: Peter Zijlstra AuthorDate: Fri, 09 Jun 2023 16:54:54 +02:00 Committer: Ingo Molnar CommitterDate: Wed, 13 Sep 2023 15:01:19 +02:00 sched: Simplify syscalls Use guards to reduce gotos and simplify control flow. Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Ingo Molnar --- kernel/sched/core.c | 154 ++++++++++++++++++------------------------- 1 file changed, 68 insertions(+), 86 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index fa57a56..67c32c4 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -7506,6 +7506,21 @@ static struct task_struct *find_process_by_pid(pid_t= pid) return pid ? find_task_by_vpid(pid) : current; } =20 +static struct task_struct *find_get_task(pid_t pid) +{ + struct task_struct *p; + guard(rcu)(); + + p =3D find_process_by_pid(pid); + if (likely(p)) + get_task_struct(p); + + return p; +} + +DEFINE_CLASS(find_get_task, struct task_struct *, if (_T) put_task_struct(= _T), + find_get_task(pid), pid_t pid) + /* * sched_setparam() passes in -1 for its policy, to let the functions * it calls know not to change it. @@ -7543,14 +7558,11 @@ static void __setscheduler_params(struct task_struc= t *p, static bool check_same_owner(struct task_struct *p) { const struct cred *cred =3D current_cred(), *pcred; - bool match; + guard(rcu)(); =20 - rcu_read_lock(); pcred =3D __task_cred(p); - match =3D (uid_eq(cred->euid, pcred->euid) || - uid_eq(cred->euid, pcred->uid)); - rcu_read_unlock(); - return match; + return (uid_eq(cred->euid, pcred->euid) || + uid_eq(cred->euid, pcred->uid)); } =20 /* @@ -7962,27 +7974,17 @@ static int do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *pa= ram) { struct sched_param lparam; - struct task_struct *p; - int retval; =20 if (!param || pid < 0) return -EINVAL; if (copy_from_user(&lparam, param, sizeof(struct sched_param))) return -EFAULT; =20 - rcu_read_lock(); - retval =3D -ESRCH; - p =3D find_process_by_pid(pid); - if (likely(p)) - get_task_struct(p); - rcu_read_unlock(); - - if (likely(p)) { - retval =3D sched_setscheduler(p, policy, &lparam); - put_task_struct(p); - } + CLASS(find_get_task, p)(pid); + if (!p) + return -ESRCH; =20 - return retval; + return sched_setscheduler(p, policy, &lparam); } =20 /* @@ -8078,7 +8080,6 @@ SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sch= ed_attr __user *, uattr, unsigned int, flags) { struct sched_attr attr; - struct task_struct *p; int retval; =20 if (!uattr || pid < 0 || flags) @@ -8093,21 +8094,14 @@ SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct s= ched_attr __user *, uattr, if (attr.sched_flags & SCHED_FLAG_KEEP_POLICY) attr.sched_policy =3D SETPARAM_POLICY; =20 - rcu_read_lock(); - retval =3D -ESRCH; - p =3D find_process_by_pid(pid); - if (likely(p)) - get_task_struct(p); - rcu_read_unlock(); + CLASS(find_get_task, p)(pid); + if (!p) + return -ESRCH; =20 - if (likely(p)) { - if (attr.sched_flags & SCHED_FLAG_KEEP_PARAMS) - get_params(p, &attr); - retval =3D sched_setattr(p, &attr); - put_task_struct(p); - } + if (attr.sched_flags & SCHED_FLAG_KEEP_PARAMS) + get_params(p, &attr); =20 - return retval; + return sched_setattr(p, &attr); } =20 /** @@ -8125,16 +8119,17 @@ SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid) if (pid < 0) return -EINVAL; =20 - retval =3D -ESRCH; - rcu_read_lock(); + guard(rcu)(); p =3D find_process_by_pid(pid); - if (p) { - retval =3D security_task_getscheduler(p); - if (!retval) - retval =3D p->policy - | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0); + if (!p) + return -ESRCH; + + retval =3D security_task_getscheduler(p); + if (!retval) { + retval =3D p->policy; + if (p->sched_reset_on_fork) + retval |=3D SCHED_RESET_ON_FORK; } - rcu_read_unlock(); return retval; } =20 @@ -8155,30 +8150,23 @@ SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct = sched_param __user *, param) if (!param || pid < 0) return -EINVAL; =20 - rcu_read_lock(); - p =3D find_process_by_pid(pid); - retval =3D -ESRCH; - if (!p) - goto out_unlock; + scoped_guard (rcu) { + p =3D find_process_by_pid(pid); + if (!p) + return -ESRCH; =20 - retval =3D security_task_getscheduler(p); - if (retval) - goto out_unlock; + retval =3D security_task_getscheduler(p); + if (retval) + return retval; =20 - if (task_has_rt_policy(p)) - lp.sched_priority =3D p->rt_priority; - rcu_read_unlock(); + if (task_has_rt_policy(p)) + lp.sched_priority =3D p->rt_priority; + } =20 /* * This one might sleep, we cannot do it with a spinlock held ... */ - retval =3D copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; - - return retval; - -out_unlock: - rcu_read_unlock(); - return retval; + return copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; } =20 /* @@ -8238,39 +8226,33 @@ SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct s= ched_attr __user *, uattr, usize < SCHED_ATTR_SIZE_VER0 || flags) return -EINVAL; =20 - rcu_read_lock(); - p =3D find_process_by_pid(pid); - retval =3D -ESRCH; - if (!p) - goto out_unlock; + scoped_guard (rcu) { + p =3D find_process_by_pid(pid); + if (!p) + return -ESRCH; =20 - retval =3D security_task_getscheduler(p); - if (retval) - goto out_unlock; + retval =3D security_task_getscheduler(p); + if (retval) + return retval; =20 - kattr.sched_policy =3D p->policy; - if (p->sched_reset_on_fork) - kattr.sched_flags |=3D SCHED_FLAG_RESET_ON_FORK; - get_params(p, &kattr); - kattr.sched_flags &=3D SCHED_FLAG_ALL; + kattr.sched_policy =3D p->policy; + if (p->sched_reset_on_fork) + kattr.sched_flags |=3D SCHED_FLAG_RESET_ON_FORK; + get_params(p, &kattr); + kattr.sched_flags &=3D SCHED_FLAG_ALL; =20 #ifdef CONFIG_UCLAMP_TASK - /* - * This could race with another potential updater, but this is fine - * because it'll correctly read the old or the new value. We don't need - * to guarantee who wins the race as long as it doesn't return garbage. - */ - kattr.sched_util_min =3D p->uclamp_req[UCLAMP_MIN].value; - kattr.sched_util_max =3D p->uclamp_req[UCLAMP_MAX].value; + /* + * This could race with another potential updater, but this is fine + * because it'll correctly read the old or the new value. We don't need + * to guarantee who wins the race as long as it doesn't return garbage. + */ + kattr.sched_util_min =3D p->uclamp_req[UCLAMP_MIN].value; + kattr.sched_util_max =3D p->uclamp_req[UCLAMP_MAX].value; #endif - - rcu_read_unlock(); + } =20 return sched_attr_copy_to_user(uattr, &kattr, usize); - -out_unlock: - rcu_read_unlock(); - return retval; } =20 #ifdef CONFIG_SMP