Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4621527iob; Sun, 8 May 2022 19:32:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxOwfEP6j/D2zOnGH16GJ0lWeEWi72nxs8JdSltWmpdhVeIe8AVQHOJ2uHffwaEt8XiRZQZ X-Received: by 2002:a17:90a:8407:b0:1d9:ab62:bd3c with SMTP id j7-20020a17090a840700b001d9ab62bd3cmr16035500pjn.139.1652063543228; Sun, 08 May 2022 19:32:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652063543; cv=none; d=google.com; s=arc-20160816; b=CN1MTTJxP/cj88fsJcr3HgEExbGMaVmlb3VXCaqNhrfAGAzaxXvpgEJlpcC6owrROD bxxZwV4Dx/pON9G03/hfSix3R3ld8dfNLo306WSRfkUaFujE1pC1nBrTp2Vb61Q0qSSA 77lDEcxKRIllkFUA3W2XBm9rtPzwZWeJczI9HmDl+KmcS8Eu4Q8+DJToBbS0Ddl1xWft AWssQvs8s2qd6J1szl3QF3HrTwU++iWJsPPLq+FGHtvr6FaprFkVu/s2lDVwIixaU/y6 O1fu0nLi1MErPWm2n3Qe9wUq8Vjr8Ys4bD7qRb9yNFdiRR6xg5K2cO3cDGvJbsmNqOG5 ykoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=bSE7VVBnWsfXxxTEbF0LrnB6UoyHMPPc7LdLn8ugniU=; b=eKDwhw7RXap0w3AdZAyXlZnqpD9yCvShJmp26pEYOkF+CxBAFPQI9YZ7K9t80qU5lO LcttMJYMWAcPiz0mYvO1UAzr2sMfiZGXRI/9ck/pVj6z9BSKEl73evIl3MAbYarIqPew 7agA5Ia3VfzXgtiG+B9A0AHfGrbue7cGlDD1rnk6iFqGCAsZ+8knuiFPKJHnoWfGuAHa rGTPuBkvRxzmhShF+vNoNqfz6Q0/UrS0Qhljq9o5NJYNB6SgpGtIQVcwRr11SLT24dKF wMivIHRphr8DbKJk7Tasv1waeJQw/nvp6dt7XbpRaH0zxvLInHSIEF5q9hWZ/zjAWLEa 813w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gF1QoGR7; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id r4-20020a170902be0400b0015415173078si9117577pls.220.2022.05.08.19.32.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 19:32:23 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gF1QoGR7; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 458FF703C8; Sun, 8 May 2022 19:31:09 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379143AbiEENWQ (ORCPT + 99 others); Thu, 5 May 2022 09:22:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379071AbiEENWA (ORCPT ); Thu, 5 May 2022 09:22:00 -0400 Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [170.10.129.74]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 80E4A4C42F for ; Thu, 5 May 2022 06:18:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651756699; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bSE7VVBnWsfXxxTEbF0LrnB6UoyHMPPc7LdLn8ugniU=; b=gF1QoGR7tTgdJDqcX5OoRNA8K+kU7UODcTsgiDypVkuLnhHei0pwRiiqB8ajPUC//IFOmZ Xndbb+y5djTFkKyOY26cDxYtCEvOPQiSRgbbyw5jsNT2MmY5Aj9EFdfqT/KDFzzLjHxPRc SMkD5/aEpEGv3kfNtzTmflrQSUOT1kM= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-139-JhhDd-uOM3eVMiD_xGdGGw-1; Thu, 05 May 2022 09:18:14 -0400 X-MC-Unique: JhhDd-uOM3eVMiD_xGdGGw-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 527CD1C05AF3; Thu, 5 May 2022 13:18:14 +0000 (UTC) Received: from shodan.usersys.redhat.com (unknown [10.43.17.22]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1589B463EC0; Thu, 5 May 2022 13:18:14 +0000 (UTC) Received: by shodan.usersys.redhat.com (Postfix, from userid 1000) id 04BE41C00FC; Thu, 5 May 2022 15:18:12 +0200 (CEST) From: Artem Savkov To: Thomas Gleixner , Josh Poimboeuf Cc: Anna-Maria Behnsen , "David S. Miller" , Hideaki YOSHIFUJI , David Ahern , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Artem Savkov Subject: [PATCH v5 1/2] timer: add a function to adjust timeouts to be upper bound Date: Thu, 5 May 2022 15:18:10 +0200 Message-Id: <20220505131811.3744503-2-asavkov@redhat.com> In-Reply-To: <20220505131811.3744503-1-asavkov@redhat.com> References: <87zgkwjtq2.ffs@tglx> <20220505131811.3744503-1-asavkov@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 Current timer wheel implementation is optimized for performance and energy usage but lacks in precision. This, normally, is not a problem as most timers that use timer wheel are used for timeouts and thus rarely expire, instead they often get canceled or modified before expiration. Even when they don't, expiring a bit late is not an issue for timeout timers. TCP keepalive timer is a special case, it's aim is to prevent timeouts, so triggering earlier rather than later is desired behavior. In a reported case the user had a 3600s keepalive timer for preventing firewall disconnects (on a 3650s interval). They observed keepalive timers coming in up to four minutes late, causing unexpected disconnects. Add a new upper_bound_timeout() function that takes a relative timeout and adjusts it based on timer wheel granularity so that supplied value effectively becomes an upper bound for the timer. This was previously discussed here: https://lore.kernel.org/all/20210302001054.4qgrvnkltvkgikzr@treble/T/#u Suggested-by: Josh Poimboeuf Signed-off-by: Artem Savkov --- include/linux/timer.h | 1 + kernel/time/timer.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/linux/timer.h b/include/linux/timer.h index fda13c9d1256..b209d31d543f 100644 --- a/include/linux/timer.h +++ b/include/linux/timer.h @@ -168,6 +168,7 @@ static inline int timer_pending(const struct timer_list * timer) return !hlist_unhashed_lockless(&timer->entry); } +extern unsigned long upper_bound_timeout(unsigned long timeout); extern void add_timer_on(struct timer_list *timer, int cpu); extern int del_timer(struct timer_list * timer); extern int mod_timer(struct timer_list *timer, unsigned long expires); diff --git a/kernel/time/timer.c b/kernel/time/timer.c index 9dd2a39cb3b0..b087a481d06f 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -545,6 +545,20 @@ static int calc_wheel_index(unsigned long expires, unsigned long clk, return idx; } +/** + * upper_bound_timeout - return adjusted timeout + * @timeout: timeout value in jiffies + * + * This function return supplied timeout adjusted to timer wheel granularity + * effectively making supplied value an upper bound at which the timer will + * expire. + */ +unsigned long upper_bound_timeout(unsigned long timeout) +{ + return timeout - (timeout >> LVL_CLK_SHIFT); +} +EXPORT_SYMBOL(upper_bound_timeout); + static void trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer) { -- 2.34.1