From patchwork Tue Sep 12 10:28:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 138185 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp410301vqx; Tue, 12 Sep 2023 06:33:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEz4lDbjzZcDlM2R9nkVZPlRa2dqLxrmfbh1sPvUCn6XAMz9p9WFHD+wHAj13EELPjPH8hl X-Received: by 2002:a17:902:eec6:b0:1c3:a2ea:64d3 with SMTP id h6-20020a170902eec600b001c3a2ea64d3mr6002375plb.41.1694525590806; Tue, 12 Sep 2023 06:33:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694525590; cv=none; d=google.com; s=arc-20160816; b=orDYqpbJ8zkqM4mSpbU232b11Q0tnlKpr5NKrmuC+usgZBKqW4EuTiyY0ONgd/5Ati W5VtgGMtVej6KjwurX2HrVBPV4M2teMxw7FVaz67fVDCv4wm4499IIcJzUWAMMvi4SvU VO0MjAtNLvmMzzsehDADRwy8kKtNAArYrG/58NaxSf3wkUPP7TejWgj1+GypkIoGkxF8 8UR9iNoEl/0X4Epnf/xZ+AF7AJ1Xjj8O8ovL0h33kUWyh+My1F/Gksj4/Wy9ovINPD+3 4sD2lxaTlMBYivTD8110T1zP6KC6/cW+fZvQhIF6fqi3ae/P1cEQDbktVV63w8SjVSb0 fH0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:cc:subject:to:reply-to:sender:from :dkim-signature:dkim-signature:date; bh=rC7fJ3unKfH5ys7/EHhA1f5zHs+G6x74lKhYIE8XIZg=; fh=H2MkSknZKy/XcUE0SNWlqZifRHvGVZQUr5cmv0Cbtz8=; b=tjaR0kcZOwCTArzB/tDg7AqR+AV1+Yu760ij30rV6kiPigKDru0jpbG3jWOzbBnGQu dabSBNWtDLrEXbOJSK34r+nWdeyevlzDyn1Z4AgO9g/H/mpqZKPmDZLMyYuOUXLJXB2E F2Y55h1YNCpRS5gpzDqNWiGtiUab2P2EOsjrAiM1SMlXvgrz0yjzn7E8XxTsJKEZP2f7 RY+uFxA286prYXn/WVYOgnXFITeBP3i3+CEf8Qm9zvZwtktzADIphWqu9Cc8phfFKw3r zTqu2QQrkjnBin0fys6zYlvOOGU8wgybajXyZ8NwaliuJvSey199jOFKNSOhLoQBfbgB gjyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="ph2MQdU/"; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id s16-20020a17090302d000b001b9dda4e1e7si7970840plk.154.2023.09.12.06.33.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 06:33:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="ph2MQdU/"; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 500E280B1809; Tue, 12 Sep 2023 03:34:53 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234280AbjILKee (ORCPT + 37 others); Tue, 12 Sep 2023 06:34:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234387AbjILKah (ORCPT ); Tue, 12 Sep 2023 06:30:37 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A4A31724; Tue, 12 Sep 2023 03:29:01 -0700 (PDT) Date: Tue, 12 Sep 2023 10:28:59 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694514539; 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=rC7fJ3unKfH5ys7/EHhA1f5zHs+G6x74lKhYIE8XIZg=; b=ph2MQdU/nZ53WVCV6Gw2CLJ/0ppDu3jr6Rdn3a6FL+4WkmfvVcIeQeowSPWJ8MU87LOdm+ SrhF4EMfKyQXsxPVWoA5KyYZy8jGkL6SjvlbscGJGYZpd5a/C0pCYnhNFcQ344T8IyerDj G7U6EqMEgH80Wu+k0MDICa9pM+59u+TeLNfaLZGAviqTKOWks9JGF2syAAMWHeKfN2G4Tv Eeu97Qt2eIa+oYiqqZsa16udmdzyE0N2y2+TVPAIYIacX8OtH7lb8l2bEzyYHVmbh/DCl8 zSTv/j+Bu0idPONo4bIqWRaIC8C8EHWIM8kZbb+umyyKp3E2jMhdtGjYcKO1jg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694514539; 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=rC7fJ3unKfH5ys7/EHhA1f5zHs+G6x74lKhYIE8XIZg=; b=BwOZw6w/gmfTtyAdLKh29jVgroF6pFmP8XUHGA4+BO7pVHi9w1Qp4c2k9vt+s6srrTZJvq d+91SW7tukwAV+CQ== 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 sched_{set,get}affinity() Cc: "Peter Zijlstra (Intel)" , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <169451453926.27769.12507870986222267887.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 12 Sep 2023 03:34:53 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776838866101275034 X-GMAIL-MSGID: 1776838866101275034 The following commit has been merged into the sched/core branch of tip: Commit-ID: d2064a0466d38e7e312aecf815cef01b5f342ad9 Gitweb: https://git.kernel.org/tip/d2064a0466d38e7e312aecf815cef01b5f342ad9 Author: Peter Zijlstra AuthorDate: Fri, 09 Jun 2023 16:57:35 +02:00 Committer: root CommitterDate: Sat, 09 Sep 2023 15:10:15 +02:00 sched: Simplify sched_{set,get}affinity() Use guards to reduce gotos and simplify control flow. Signed-off-by: Peter Zijlstra (Intel) --- kernel/sched/core.c | 53 +++++++++++--------------------------------- 1 file changed, 14 insertions(+), 39 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index db5cf31..a9b9fa9 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -8348,39 +8348,24 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) { struct affinity_context ac; struct cpumask *user_mask; - struct task_struct *p; int retval; - rcu_read_lock(); - - p = find_process_by_pid(pid); - if (!p) { - rcu_read_unlock(); + CLASS(find_get_task, p)(pid); + if (!p) return -ESRCH; - } - - /* Prevent p going away */ - get_task_struct(p); - rcu_read_unlock(); - if (p->flags & PF_NO_SETAFFINITY) { - retval = -EINVAL; - goto out_put_task; - } + if (p->flags & PF_NO_SETAFFINITY) + return -EINVAL; if (!check_same_owner(p)) { - rcu_read_lock(); - if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) { - rcu_read_unlock(); - retval = -EPERM; - goto out_put_task; - } - rcu_read_unlock(); + guard(rcu)(); + if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) + return -EPERM; } retval = security_task_setscheduler(p); if (retval) - goto out_put_task; + return retval; /* * With non-SMP configs, user_cpus_ptr/user_mask isn't used and @@ -8390,8 +8375,7 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) if (user_mask) { cpumask_copy(user_mask, in_mask); } else if (IS_ENABLED(CONFIG_SMP)) { - retval = -ENOMEM; - goto out_put_task; + return -ENOMEM; } ac = (struct affinity_context){ @@ -8403,8 +8387,6 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) retval = __sched_setaffinity(p, &ac); kfree(ac.user_mask); -out_put_task: - put_task_struct(p); return retval; } @@ -8446,28 +8428,21 @@ SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len, long sched_getaffinity(pid_t pid, struct cpumask *mask) { struct task_struct *p; - unsigned long flags; int retval; - rcu_read_lock(); - - retval = -ESRCH; + guard(rcu)(); p = find_process_by_pid(pid); if (!p) - goto out_unlock; + return -ESRCH; retval = security_task_getscheduler(p); if (retval) - goto out_unlock; + return retval; - raw_spin_lock_irqsave(&p->pi_lock, flags); + guard(raw_spinlock_irqsave)(&p->pi_lock); cpumask_and(mask, &p->cpus_mask, cpu_active_mask); - raw_spin_unlock_irqrestore(&p->pi_lock, flags); -out_unlock: - rcu_read_unlock(); - - return retval; + return 0; } /**