Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp2225390ybh; Fri, 24 Jul 2020 07:32:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzJnbm9ndRXM75K1OiXsahs6FtBEGLMTH4jobZsZnzAVGfuWPn18g5iqF7lGh3B0rgqJWBG X-Received: by 2002:a05:6402:337:: with SMTP id q23mr9386174edw.63.1595601148801; Fri, 24 Jul 2020 07:32:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595601148; cv=none; d=google.com; s=arc-20160816; b=FCR9IpqtRGrKVJVwOa17vdOCXqKY+U/hgratN8C9zN+rSo0OozCv2ejkSJGtp8KPQa ZuFTLaXChTwlVRm5NNgeQBcqtxcEs6evTbBdpZSoI1eMcoe1YcluZ5ybRkvTolaMrNSq BzEwRVkxsBssDr6Wr+/1wDSJPw9G8hHbEBj3DYWcocmjdUjnmRvyjxA/uDKye22NRQkq fL1Y/qOODGh7LxcEaZQQpOAFyFr28Ad/hD5iNRPh9bfws8f01YiFad6ifoAUvGq/a++A zwHTenmhXkqMV6sVscM7VzFUMaIucDUqJMYG9znjfnO1/zQ+f6GibMEYCJX7W5F9SZff IWNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=623/gE+t/NAauC48CDPw5iNlUH9MVtSJpPc426h2K5M=; b=GRl6BGS4Lw3LIyYE0IjQkJIQYdFeyhj4a0MhlNh7SQtifGTYtGY85pZN6OtXl/+pa4 CVrRpx9qB2y7uIqUm0YMGkSApBrw2RX2YyfJuAFXBk4iCltsuRVH91w2TAv1L416uPMv Tc7gf7l8Ktcn8OoFsgZwT1WVNiD8PQcor5UOn7nPBz1UB7Ew3sLNVeuua93Msz13F83v /UmyKNiTfLb1gDxPIXO08AXmF6C5+lQzFxlIdf+UXDtIMxf2lxRD35QOxk8r/Et5XB4s hU5HU0OUEtBQ5k+ywaiqbHAHq/znedtu1D6Ru2GeDucGLGSIfoYILt/uIoItaYCzJFsA 5Xuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=kN2nzfwu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q17si685203eja.355.2020.07.24.07.32.06; Fri, 24 Jul 2020 07:32:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=kN2nzfwu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726617AbgGXObn (ORCPT + 99 others); Fri, 24 Jul 2020 10:31:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726326AbgGXObm (ORCPT ); Fri, 24 Jul 2020 10:31:42 -0400 Received: from mail-qv1-xf44.google.com (mail-qv1-xf44.google.com [IPv6:2607:f8b0:4864:20::f44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8224DC0619D3 for ; Fri, 24 Jul 2020 07:31:42 -0700 (PDT) Received: by mail-qv1-xf44.google.com with SMTP id el4so4162689qvb.13 for ; Fri, 24 Jul 2020 07:31:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=623/gE+t/NAauC48CDPw5iNlUH9MVtSJpPc426h2K5M=; b=kN2nzfwuzsH+t/mKkGCZ6ygMIN/RvaebEl1PWSwAXMxnqa6f9DLQRpnr4OnjeVBN87 8/HhCpuMuw+Dox9Q2uqS/XHHB3klUIU7zuwA20kO+oMUlY+XppcxPx1sbquLV+raa/JW 7VPvolE+rKqRLYqyJKEuN5FKGIg7hOQr5IRdHGq39qN2h1JU6/07+MI4B1nQGBkOwAtA Ojg4I7avozPqahwPeEWugPQVI1DOpO888LCN9dItKMJo/GJiEakjlylcXc02RDto+5LY GIddrTeP0mcLteGk1ph978PwTrSC6/pPNmj10tWa5/RPxlkY9xJnFg2gUEdZ6HmWKy6C UizA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=623/gE+t/NAauC48CDPw5iNlUH9MVtSJpPc426h2K5M=; b=lPV8kdNy2QafhmgahA2rPJd+BjNHSw0ZlgvghdoZQo3DHpd0PLV0X+bfkHE9Yjhf+P qxAmvDQgw0m4x0fnqsLWKOtbHf/iF+nDPuNZFU4a/4jy6Pb01dv7YoeQg6m6QP8rEMpA OieaMDSVRLGOo9brdeAZ3tfXFrYLkbOHC66hECwlTmijbVM+ORW6FRP/yFTUmknLOBqD ZBuibKP9gb96yCOahY6jqhbANve6lkkBF9HAu37kasT7yyzHtVfX3RVnGhKFRdTtJy4p va+BHHfPE6pQQyx+zd0CAadZZi/yQ+1AWp+qSZ9eluy+aamEfJIbQkP5bnuVH8tqcgLS fbcQ== X-Gm-Message-State: AOAM531R6L+q4o7BwH5ksOupS67ggf6BCYaKKgXpjUEpV0zaSaNBSaBT dwoXA6tXqkXosZbM20tg/+A= X-Received: by 2002:ad4:4869:: with SMTP id u9mr10018190qvy.98.1595601101763; Fri, 24 Jul 2020 07:31:41 -0700 (PDT) Received: from dev.localdomain ([183.134.211.52]) by smtp.gmail.com with ESMTPSA id y50sm1664233qtk.29.2020.07.24.07.31.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 24 Jul 2020 07:31:41 -0700 (PDT) From: qianjun.kernel@gmail.com To: tglx@linutronix.de, peterz@infradead.org, will@kernel.org, luto@kernel.org Cc: linux-kernel@vger.kernel.org, laoar.shao@gmail.com, urezki@gmail.com, jun qian Subject: [PATCH V4] Softirq:avoid large sched delay from the pending softirqs Date: Fri, 24 Jul 2020 10:31:23 -0400 Message-Id: <1595601083-10183-1-git-send-email-qianjun.kernel@gmail.com> X-Mailer: git-send-email 1.8.3.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: jun qian When get the pending softirqs, it need to process all the pending softirqs in the while loop. If the processing time of each pending softirq is need more than 2 msec in this loop, or one of the softirq will running a long time, according to the original code logic, it will process all the pending softirqs without wakeuping ksoftirqd, which will cause a relatively large scheduling delay on the corresponding CPU, which we do not wish to see. The patch will check the total time to process pending softirq, if the time exceeds 2 ms we need to wakeup the ksofirqd to aviod large sched delay. Signed-off-by: jun qian --- kernel/softirq.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/kernel/softirq.c b/kernel/softirq.c index c4201b7f..d572ce4 100644 --- a/kernel/softirq.c +++ b/kernel/softirq.c @@ -25,6 +25,7 @@ #include #include #include +#include #define CREATE_TRACE_POINTS #include @@ -200,17 +201,15 @@ void __local_bh_enable_ip(unsigned long ip, unsigned int cnt) /* * We restart softirq processing for at most MAX_SOFTIRQ_RESTART times, * but break the loop if need_resched() is set or after 2 ms. - * The MAX_SOFTIRQ_TIME provides a nice upper bound in most cases, but in - * certain cases, such as stop_machine(), jiffies may cease to - * increment and so we need the MAX_SOFTIRQ_RESTART limit as - * well to make sure we eventually return from this method. + * In the loop, if the processing time of the softirq has exceeded 2 + * milliseconds, we also need to break the loop to wakeup the ksofirqd. * * These limits have been established via experimentation. * The two things to balance is latency against fairness - * we want to handle softirqs as soon as possible, but they * should not be able to lock up the box. */ -#define MAX_SOFTIRQ_TIME msecs_to_jiffies(2) +#define MAX_SOFTIRQ_TIME_NS 2000000 #define MAX_SOFTIRQ_RESTART 10 #ifdef CONFIG_TRACE_IRQFLAGS @@ -248,7 +247,7 @@ static inline void lockdep_softirq_end(bool in_hardirq) { } asmlinkage __visible void __softirq_entry __do_softirq(void) { - unsigned long end = jiffies + MAX_SOFTIRQ_TIME; + u64 end = sched_clock() + MAX_SOFTIRQ_TIME_NS; unsigned long old_flags = current->flags; int max_restart = MAX_SOFTIRQ_RESTART; struct softirq_action *h; @@ -299,6 +298,19 @@ asmlinkage __visible void __softirq_entry __do_softirq(void) } h++; pending >>= softirq_bit; + + /* + * the softirq's action has been running for too much time + * so it may need to wakeup the ksoftirqd + */ + if (need_resched() && sched_clock() > end) { + /* + * Ensure that the remaining pending bits are + * handled. + */ + or_softirq_pending(pending << (vec_nr + 1)); + break; + } } if (__this_cpu_read(ksoftirqd) == current) @@ -307,8 +319,8 @@ asmlinkage __visible void __softirq_entry __do_softirq(void) pending = local_softirq_pending(); if (pending) { - if (time_before(jiffies, end) && !need_resched() && - --max_restart) + if (!need_resched() && --max_restart && + sched_clock() <= end) goto restart; wakeup_softirqd(); -- 1.8.3.1