Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp4692578rwb; Tue, 20 Sep 2022 18:50:33 -0700 (PDT) X-Google-Smtp-Source: AMsMyM655q/tavxOfLXtNGHvEDuVRYWsG8TG+HwXGRm+mCKi4zrzful0P9/WezmEOoJmL2063Fku X-Received: by 2002:a17:907:25cd:b0:77b:9672:38e7 with SMTP id ae13-20020a17090725cd00b0077b967238e7mr19528798ejc.10.1663725032858; Tue, 20 Sep 2022 18:50:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663725032; cv=none; d=google.com; s=arc-20160816; b=znfxhty5ISHEJIoiDsgO7hRTmwxKiv0cGTpxA71BvxE/K+C71XvPmub9bWUkZuFWeL OapYdIBRzWQW+9xLKkyTau9tdrxqCQtqxg2agjTyudrsclbrlv14bwtveCA7A9bs40zl iyttT5y2V307SnoCJ+BSLZIqItoAbyCFZxMLKGfQYFBST00C07A2DeUUH5by89QrYPqe tiTzFw0oyg/DHRFjU1SH/RORip5NQRmiKAWMcKg3a4TH2xwJcgb+Znoz+IzBFCtSLne4 gobDrFPHFHgDWVGdgOxP4ak1CjmEqbMDlAM+WcPjl/+3svy7xD/hZyoNC+vMs8nPRWb5 071w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=o4GP7+7x/i3LtS7KDRY9k8uFn9y3A3mtuqvfdAnDEoY=; b=N8QmSzZjdEeH4ZY6u+xyvUCTEpFL1oL5j9JWR7p8SV2KO/eOXqmLaJqG9TxSnKt45E XFmjCq1vCP7UDKD2g5f6cmyLJDgnSrAuOl0nVGUFdjsRXoivrmCYLTLTk8dWC6wigH2V cI+4Mcxc4QHnxGTR/0wNkF6GDm0bbiHpHj5sTvYhpDkUTe/pR1lf605/mZfCE0PmntOO GmzxhThDKrr8gVXmqX0zig97VfpoReun8ql2KIrqbrYN4XurtoAbR9dgNjEEAY0Fd7Y8 t8kewJqqUhAceuqYXtb7dRO6H87j2AmkX2HxCpvuHH01/HvVrXRDpuhKAvtR1PxiEPeb PE9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=fplXy7xM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u17-20020a50c051000000b0043e81c582d2si1231502edd.232.2022.09.20.18.50.07; Tue, 20 Sep 2022 18:50:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=fplXy7xM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230373AbiIUB1g (ORCPT + 99 others); Tue, 20 Sep 2022 21:27:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231620AbiIUB1D (ORCPT ); Tue, 20 Sep 2022 21:27:03 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92FD92AC4D for ; Tue, 20 Sep 2022 18:26:14 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id k2-20020a170902c40200b001782bd6c416so2811926plk.20 for ; Tue, 20 Sep 2022 18:26:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=o4GP7+7x/i3LtS7KDRY9k8uFn9y3A3mtuqvfdAnDEoY=; b=fplXy7xMgyYMI0/pnBdIDKGKPtgJG9PvrNQ89OyMBQQMHbmB9zpEMFcCoYHvKLSUOT R1ID/N2FjdFsSkIt27kDa5UWzPyWUh237BWk1qhWjSZ3noAmHpMqIcxwChCkZy7MtCV/ RrBeb1KmC3esXIM4nmuo2j8wtLEyHCvfErS5d2+ICdj8wGu0ok/WLFbt7+9ApfaLAprw hgqMAV9SbFZIIovy2eSHC8DnqKh1nstwiQat4up9D3YaIcoccgcIpZ4VvMXTl0yxRgQr FD/ZsrthLVzgVI+5+wEoBrWLK2qHbMXcqi6QGlJj6tXoZ0BM45Oco3LrhthlmajSQSEG ObSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=o4GP7+7x/i3LtS7KDRY9k8uFn9y3A3mtuqvfdAnDEoY=; b=WbVr8dffVv4CyhvFjhyZYu+g450kxkJQgF8a67g4F/EmcW+OOkdY3owT18roeSsmFR oIOdrAnl+Gos4XFXclKV2FcrgP0we3PCE96n50fjOlAUe+4H4cqlgCT5xnDpA05p1xAe 3jNs/7ai43Yv1487lbYuIEMbgyCBNbz1QNEFsKKCBv5SMMteNe8vEfYCtXb6bQEYnM/j RCM09PaxpWQi8tro492a2dxMOadsO2PCX6noEF29gd1Z4LaUz34lqKDFqBpn6RtAiseT bCFJapMsptHoEChp59MYj0MeUDbwTVrQKJMqcxSO0QVzN8P/lrcRQsnkEqTFw7wRvTTW Ff6A== X-Gm-Message-State: ACrzQf2M7KDpG1Uyieo2D65c1gu1UjVivjibbELwU/gofWZAZRRbIiuj f/CJyS+cEdfcLgU9ir9Rkmxd0wiW+nXy5Qt6TOYZ3A0o8GW20YesPZK/FvsHratrBNqsv+m94MD FMjD1ouwybh2bQldLbmBcr3qGuX1A9lmkuXzErcUb1MXLHRt7kdVgCGolSTKi9K7UfT4jX+4= X-Received: from jstultz-noogler2.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:600]) (user=jstultz job=sendgmr) by 2002:a62:1b8f:0:b0:54b:8114:e762 with SMTP id b137-20020a621b8f000000b0054b8114e762mr22951951pfb.7.1663723560518; Tue, 20 Sep 2022 18:26:00 -0700 (PDT) Date: Wed, 21 Sep 2022 01:25:50 +0000 In-Reply-To: <20220921012550.3288570-1-jstultz@google.com> Mime-Version: 1.0 References: <20220921012550.3288570-1-jstultz@google.com> X-Mailer: git-send-email 2.37.3.968.ga6b4b080e4-goog Message-ID: <20220921012550.3288570-4-jstultz@google.com> Subject: [RFC PATCH v3 3/3] softirq: defer softirq processing to ksoftirqd if CPU is busy with RT From: John Stultz To: LKML Cc: Pavankumar Kondeti , John Dias , "Connor O'Brien" , Rick Yiu , John Kacur , Qais Yousef , Chris Redpath , Abhijeet Dharmapurikar , Peter Zijlstra , Ingo Molnar , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Thomas Gleixner , kernel-team@android.com, Satya Durga Srinivasu Prabhala , "J . Avila" , John Stultz Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Pavankumar Kondeti Defer the softirq processing to ksoftirqd if a RT task is running or queued on the current CPU. This complements the RT task placement algorithm which tries to find a CPU that is not currently busy with softirqs. Currently NET_TX, NET_RX, BLOCK and TASKLET softirqs are only deferred as they can potentially run for long time. Additionally, this patch stubs out ksoftirqd_running() logic, in the CONFIG_RT_SOFTIRQ_OPTIMIZATION case, as deferring potentially long-running softirqs will cause the logic to not process shorter-running softirqs immediately. By stubbing it out the potentially long running softirqs are deferred, but the shorter running ones can still run immediately. This patch includes folded-in fixes by: Lingutla Chandrasekhar Satya Durga Srinivasu Prabhala J. Avila Cc: John Dias Cc: Connor O'Brien Cc: Rick Yiu Cc: John Kacur Cc: Qais Yousef Cc: Chris Redpath Cc: Abhijeet Dharmapurikar Cc: Peter Zijlstra Cc: Ingo Molnar Cc: Juri Lelli Cc: Vincent Guittot Cc: Dietmar Eggemann Cc: Steven Rostedt Cc: Thomas Gleixner Cc: kernel-team@android.com Signed-off-by: Pavankumar Kondeti [satyap@codeaurora.org: trivial merge conflict resolution.] Signed-off-by: Satya Durga Srinivasu Prabhala [elavila: Port to mainline, squash with bugfix] Signed-off-by: J. Avila [jstultz: Rebase to linus/HEAD, minor rearranging of code, included bug fix Reported-by: Qais Yousef ] Signed-off-by: John Stultz --- include/linux/sched.h | 10 ++++++++++ kernel/sched/cpupri.c | 13 +++++++++++++ kernel/softirq.c | 25 +++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index e7b2f8a5c711..7f76371cbbb0 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1826,6 +1826,16 @@ current_restore_flags(unsigned long orig_flags, unsigned long flags) extern int cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial); extern int task_can_attach(struct task_struct *p, const struct cpumask *cs_effective_cpus); + +#ifdef CONFIG_RT_SOFTIRQ_OPTIMIZATION +extern bool cpupri_check_rt(void); +#else +static inline bool cpupri_check_rt(void) +{ + return false; +} +#endif + #ifdef CONFIG_SMP extern void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask); extern int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask); diff --git a/kernel/sched/cpupri.c b/kernel/sched/cpupri.c index fa9ce9d83683..18dc75d16951 100644 --- a/kernel/sched/cpupri.c +++ b/kernel/sched/cpupri.c @@ -64,6 +64,19 @@ static int convert_prio(int prio) return cpupri; } +#ifdef CONFIG_RT_SOFTIRQ_OPTIMIZATION +/* + * cpupri_check_rt - check if CPU has a RT task + * should be called from rcu-sched read section. + */ +bool cpupri_check_rt(void) +{ + int cpu = raw_smp_processor_id(); + + return cpu_rq(cpu)->rd->cpupri.cpu_to_pri[cpu] > CPUPRI_NORMAL; +} +#endif + static inline int __cpupri_find(struct cpupri *cp, struct task_struct *p, struct cpumask *lowest_mask, int idx) { diff --git a/kernel/softirq.c b/kernel/softirq.c index 35ee79dd8786..203a70dc9459 100644 --- a/kernel/softirq.c +++ b/kernel/softirq.c @@ -87,6 +87,7 @@ static void wakeup_softirqd(void) wake_up_process(tsk); } +#ifndef CONFIG_RT_SOFTIRQ_OPTIMIZATION /* * If ksoftirqd is scheduled, we do not want to process pending softirqs * right now. Let ksoftirqd handle this at its own rate, to get fairness, @@ -101,6 +102,9 @@ static bool ksoftirqd_running(unsigned long pending) return false; return tsk && task_is_running(tsk) && !__kthread_should_park(tsk); } +#else +#define ksoftirqd_running(pending) (false) +#endif /* CONFIG_RT_SOFTIRQ_OPTIMIZATION */ #ifdef CONFIG_TRACE_IRQFLAGS DEFINE_PER_CPU(int, hardirqs_enabled); @@ -532,6 +536,17 @@ static inline bool lockdep_softirq_start(void) { return false; } static inline void lockdep_softirq_end(bool in_hardirq) { } #endif +static __u32 softirq_deferred_for_rt(__u32 *pending) +{ + __u32 deferred = 0; + + if (cpupri_check_rt()) { + deferred = *pending & LONG_SOFTIRQ_MASK; + *pending &= ~LONG_SOFTIRQ_MASK; + } + return deferred; +} + asmlinkage __visible void __softirq_entry __do_softirq(void) { unsigned long end = jiffies + MAX_SOFTIRQ_TIME; @@ -539,6 +554,7 @@ asmlinkage __visible void __softirq_entry __do_softirq(void) int max_restart = MAX_SOFTIRQ_RESTART; struct softirq_action *h; bool in_hardirq; + __u32 deferred; __u32 pending; int softirq_bit; @@ -551,13 +567,15 @@ asmlinkage __visible void __softirq_entry __do_softirq(void) pending = local_softirq_pending(); + deferred = softirq_deferred_for_rt(&pending); softirq_handle_begin(); + in_hardirq = lockdep_softirq_start(); account_softirq_enter(current); restart: /* Reset the pending bitmask before enabling irqs */ - set_softirq_pending(0); + set_softirq_pending(deferred); __this_cpu_write(active_softirqs, pending); local_irq_enable(); @@ -596,13 +614,16 @@ asmlinkage __visible void __softirq_entry __do_softirq(void) local_irq_disable(); pending = local_softirq_pending(); + deferred = softirq_deferred_for_rt(&pending); + if (pending) { if (time_before(jiffies, end) && !need_resched() && --max_restart) goto restart; + } + if (pending | deferred) wakeup_softirqd(); - } account_softirq_exit(current); lockdep_softirq_end(in_hardirq); -- 2.37.3.968.ga6b4b080e4-goog