From patchwork Tue Sep 12 10:28:58 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: 138564 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp816008vqx; Tue, 12 Sep 2023 19:49:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHex7/siAre8yCvv7uRFPAkWTYOROOIFZZ0D/H9A9uf8rnk1MvTBW5WHlFR/lSMrZXXvg3G X-Received: by 2002:a05:620a:25d0:b0:770:ef43:7638 with SMTP id y16-20020a05620a25d000b00770ef437638mr1564396qko.33.1694573372069; Tue, 12 Sep 2023 19:49:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694573372; cv=none; d=google.com; s=arc-20160816; b=xCbWD63c7NtEYGPej4zH1RXzoJo9C/f27hUO+ot2ceJcNVfcvO8EotqTv3IzRTlpHT gG/JM/v6fSklz6+u8PUrT2bnN1gwS110Yn3tJT5PKqJG2CZj5wFfYIjgy9pWOTifnUkj s6ZxaJg+7i9Qf/OAIUP1eDD0WnZ05GFOnQQR56U3vZD+HOavlfl2vtKmefLKMnx35XvH T7AVErXZzhiJ7WrgrmRIFJjdCV5wMadLOO71oxclPjGVeYQrvXO3WCYs0HXvNKJ2Qhxm +hlaKAqzf6kuHHOq6W2C3OeoRF7v8XXa5vfUHkTCCi4TcFj4ujgg1+nzvpW18fRLrsMD kWKw== 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=9/c5WAScPORoGVq2FSiwIsh38H/zaJClHn7ZgyVDpdw=; fh=H2MkSknZKy/XcUE0SNWlqZifRHvGVZQUr5cmv0Cbtz8=; b=va0UZh7MwYrvXqk9IMkp2TmNu+y/kHTo67iQigyF3zLIFzsrLqIc5WEYC4m6cy6pJ8 wEdSqMc2hD0HLqD0BRgi8Pq1UiD8kj9DSMy4sFsdQMxvlx+K4R0tPCY/O6vvqNyGtIlL TQ9nPRmPNkefnkmQUOZiBcfCbPGxk1pMYC/ruzNBtrHlhjI6hhQmCMuqS5PLr5fg87EN AJuxQr3QKjP+XiST69NVrd2TlL0ED5Eo7A0omMvAEJQZBr6yuqTOXafHOkxXiNihK1AT D4Bo//bOXQxKF2ix2MjKmDEtUQRBfT6KB1gw42v6QPANLo7iOSjBAUAuUbJ9C5sMYmT6 xM6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Aa7aIz7+; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=gIslnSMu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id s24-20020a63af58000000b00577581963ccsi5889134pgo.260.2023.09.12.19.49.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 19:49:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Aa7aIz7+; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=gIslnSMu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id 9AA8285BF60B; Tue, 12 Sep 2023 03:34:33 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234248AbjILKea (ORCPT + 37 others); Tue, 12 Sep 2023 06:34:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234385AbjILKah (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 A45041723; Tue, 12 Sep 2023 03:29:00 -0700 (PDT) Date: Tue, 12 Sep 2023 10:28:58 -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=9/c5WAScPORoGVq2FSiwIsh38H/zaJClHn7ZgyVDpdw=; b=Aa7aIz7+RULKnXj50xg3WkDkp4ZsuQg7lmRDuXit0NZpFiJywvJJnenfztW9ymrPzIKEfV mxBFjYrHtzVOwkFwxh0DTgs4t7rFkMtpvXfmF7kdHneHkuovulTP7zkL+IWJVay5FS7pt5 /PjdJiuvptsbaUq1plWIpo9g7rx7ifCHJ1COohEr61m8JwBNX81r833dSNKT/zlZqtaoxY tbAQgV+oiaUHvPhgVjxsLca3gXHAaQsU7f9HherGA+OH+QOFKBplp6xlSMDWVDySsEpUUA lPwuZfETo5Gnj8bgme/bizlaYQYC1/1QOI1Y7czcDfQzSqrqD3QhDf2Pq2wAhA== 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=9/c5WAScPORoGVq2FSiwIsh38H/zaJClHn7ZgyVDpdw=; b=gIslnSMuU/6KDZEZVzo/l0EAU/CG2LL6gktjhWZWNIjw1JDwgCkt1bVoWNUynQzJmAvkOu OPyk6PxJHNWTtRBw== 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 yield_to() Cc: "Peter Zijlstra (Intel)" , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <169451453874.27769.1812735099642835156.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 (howler.vger.email [0.0.0.0]); Tue, 12 Sep 2023 03:34:33 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776888968416651552 X-GMAIL-MSGID: 1776888968416651552 The following commit has been merged into the sched/core branch of tip: Commit-ID: a5a9d756c80f46cda38952d9ccd4e810b8bdb9d7 Gitweb: https://git.kernel.org/tip/a5a9d756c80f46cda38952d9ccd4e810b8bdb9d7 Author: Peter Zijlstra AuthorDate: Fri, 09 Jun 2023 16:58:23 +02:00 Committer: root CommitterDate: Sat, 09 Sep 2023 15:10:16 +02:00 sched: Simplify yield_to() Use guards to reduce gotos and simplify control flow. Signed-off-by: Peter Zijlstra (Intel) --- kernel/sched/core.c | 67 +++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 38 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index a9b9fa9..e825839 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -8889,55 +8889,46 @@ int __sched yield_to(struct task_struct *p, bool preempt) { struct task_struct *curr = current; struct rq *rq, *p_rq; - unsigned long flags; int yielded = 0; - local_irq_save(flags); - rq = this_rq(); + scoped_guard (irqsave) { + rq = this_rq(); again: - p_rq = task_rq(p); - /* - * If we're the only runnable task on the rq and target rq also - * has only one task, there's absolutely no point in yielding. - */ - if (rq->nr_running == 1 && p_rq->nr_running == 1) { - yielded = -ESRCH; - goto out_irq; - } + p_rq = task_rq(p); + /* + * If we're the only runnable task on the rq and target rq also + * has only one task, there's absolutely no point in yielding. + */ + if (rq->nr_running == 1 && p_rq->nr_running == 1) + return -ESRCH; - double_rq_lock(rq, p_rq); - if (task_rq(p) != p_rq) { - double_rq_unlock(rq, p_rq); - goto again; - } + guard(double_rq_lock)(rq, p_rq); + if (task_rq(p) != p_rq) + goto again; - if (!curr->sched_class->yield_to_task) - goto out_unlock; + if (!curr->sched_class->yield_to_task) + return 0; - if (curr->sched_class != p->sched_class) - goto out_unlock; + if (curr->sched_class != p->sched_class) + return 0; - if (task_on_cpu(p_rq, p) || !task_is_running(p)) - goto out_unlock; + if (task_on_cpu(p_rq, p) || !task_is_running(p)) + return 0; - yielded = curr->sched_class->yield_to_task(rq, p); - if (yielded) { - schedstat_inc(rq->yld_count); - /* - * Make p's CPU reschedule; pick_next_entity takes care of - * fairness. - */ - if (preempt && rq != p_rq) - resched_curr(p_rq); + yielded = curr->sched_class->yield_to_task(rq, p); + if (yielded) { + schedstat_inc(rq->yld_count); + /* + * Make p's CPU reschedule; pick_next_entity + * takes care of fairness. + */ + if (preempt && rq != p_rq) + resched_curr(p_rq); + } } -out_unlock: - double_rq_unlock(rq, p_rq); -out_irq: - local_irq_restore(flags); - - if (yielded > 0) + if (yielded) schedule(); return yielded;