[{"id":3674057,"web_url":"http://patchwork.ozlabs.org/comment/3674057/","msgid":"<20260407100920.GT2872@noisy.programming.kicks-ass.net>","list_archive_url":null,"date":"2026-04-07T10:09:20","subject":"Re: [patch 09/12] fs/timerfd: Use the new alarm/hrtimer functions","submitter":{"id":493,"url":"http://patchwork.ozlabs.org/api/people/493/","name":"Peter Zijlstra","email":"peterz@infradead.org"},"content":"On Tue, Apr 07, 2026 at 10:54:58AM +0200, Thomas Gleixner wrote:\n\n> +static u64 timerfd_alarm_restart(struct timerfd_ctx *ctx)\n> +{\n> +\tu64 ticks = alarm_forward_now(&ctx->t.alarm, ctx->tintv) - 1;\n\n(still confused on the alarm_forward_now() vs alarmtimer_start()\nnamespacing)\n\n> +\n> +\ttimerfd_alarm_start(ctx, alarm_get_expires(&ctx->t.alarm), false);\n> +\treturn ticks;\n> +}\n> +\n> +static void timerfd_hrtimer_start(struct timerfd_ctx *ctx, ktime_t exp,\n> +\t\t\t\t  const enum hrtimer_mode mode)\n> +{\n> +\t/* Start the timer. If it's expired already, handle the callback. */\n> +\tif (!hrtimer_start_range_ns_user(&ctx->t.tmr, exp, 0, mode))\n> +\t\t__timerfd_triggered(ctx);\n> +}\n> +\n> +static u64 timerfd_hrtimer_restart(struct timerfd_ctx *ctx)\n> +{\n> +\tu64 ticks = hrtimer_forward_now(&ctx->t.tmr, ctx->tintv) - 1;\n> +\n> +\ttimerfd_hrtimer_start(ctx, hrtimer_get_expires(&ctx->t.tmr), HRTIMER_MODE_ABS);\n> +\treturn ticks;\n> +}\n\n> -\t\tif (ctx->expired && ctx->tintv) {\n> -\t\t\t/*\n> -\t\t\t * If tintv != 0, this is a periodic timer that\n> -\t\t\t * needs to be re-armed. We avoid doing it in the timer\n> -\t\t\t * callback to avoid DoS attacks specifying a very\n> -\t\t\t * short timer period.\n> -\t\t\t */\n> -\t\t\tif (isalarm(ctx)) {\n> -\t\t\t\tticks += alarm_forward_now(\n> -\t\t\t\t\t&ctx->t.alarm, ctx->tintv) - 1;\n> -\t\t\t\talarm_restart(&ctx->t.alarm);\n> -\t\t\t} else {\n> -\t\t\t\tticks += hrtimer_forward_now(&ctx->t.tmr,\n> -\t\t\t\t\t\t\t     ctx->tintv) - 1;\n> -\t\t\t\thrtimer_restart(&ctx->t.tmr);\n> -\t\t\t}\n> -\t\t}\n> +\t\tticks = ctx->ticks;\n>  \t\tctx->expired = 0;\n>  \t\tctx->ticks = 0;\n> +\n> +\t\t/*\n> +\t\t * If tintv != 0, this is a periodic timer that needs to be\n> +\t\t * re-armed. We avoid doing it in the timer callback to avoid\n> +\t\t * DoS attacks specifying a very short timer period.\n> +\t\t */\n> +\t\tif (expired && ctx->tintv)\n> +\t\t\tticks += timerfd_restart(ctx);\n>  \t}\n>  \tspin_unlock_irq(&ctx->wqh.lock);\n>  \tif (ticks) {\n> @@ -526,18 +554,7 @@ static int do_timerfd_gettime(int ufd, s\n>  \tspin_lock_irq(&ctx->wqh.lock);\n>  \tif (ctx->expired && ctx->tintv) {\n>  \t\tctx->expired = 0;\n> -\n> -\t\tif (isalarm(ctx)) {\n> -\t\t\tctx->ticks +=\n> -\t\t\t\talarm_forward_now(\n> -\t\t\t\t\t&ctx->t.alarm, ctx->tintv) - 1;\n> -\t\t\talarm_restart(&ctx->t.alarm);\n> -\t\t} else {\n> -\t\t\tctx->ticks +=\n> -\t\t\t\thrtimer_forward_now(&ctx->t.tmr, ctx->tintv)\n> -\t\t\t\t- 1;\n\n(argh!)\n\n> -\t\t\thrtimer_restart(&ctx->t.tmr);\n> -\t\t}\n> +\t\tctx->ticks += timerfd_restart(ctx);\n>  \t}\n>  \tt->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));\n>  \tt->it_interval = ktime_to_timespec64(ctx->tintv);\n\nWhat's with the -1 thing?\n\nAnyway, this looks about right.","headers":{"Return-Path":"\n <netfilter-devel+bounces-11663-incoming=patchwork.ozlabs.org@vger.kernel.org>","X-Original-To":["incoming@patchwork.ozlabs.org","netfilter-devel@vger.kernel.org"],"Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n secure) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256\n header.s=casper.20170209 header.b=dG7LFnnD;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org\n (client-ip=2600:3c15:e001:75::12fc:5321; helo=sin.lore.kernel.org;\n envelope-from=netfilter-devel+bounces-11663-incoming=patchwork.ozlabs.org@vger.kernel.org;\n receiver=patchwork.ozlabs.org)","smtp.subspace.kernel.org;\n\tdkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org\n header.b=\"dG7LFnnD\"","smtp.subspace.kernel.org;\n arc=none smtp.client-ip=90.155.50.34","smtp.subspace.kernel.org;\n dmarc=pass (p=none dis=none) header.from=infradead.org","smtp.subspace.kernel.org;\n spf=none smtp.mailfrom=infradead.org"],"Received":["from sin.lore.kernel.org (sin.lore.kernel.org\n [IPv6:2600:3c15:e001:75::12fc:5321])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4fqhkf1tDbz1xy1\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 07 Apr 2026 20:09:30 +1000 (AEST)","from smtp.subspace.kernel.org (conduit.subspace.kernel.org\n [100.90.174.1])\n\tby sin.lore.kernel.org (Postfix) with ESMTP id C6456300B8FA\n\tfor <incoming@patchwork.ozlabs.org>; Tue,  7 Apr 2026 10:09:27 +0000 (UTC)","from localhost.localdomain (localhost.localdomain [127.0.0.1])\n\tby smtp.subspace.kernel.org (Postfix) with ESMTP id E2ABD3A7F50;\n\tTue,  7 Apr 2026 10:09:25 +0000 (UTC)","from casper.infradead.org (casper.infradead.org [90.155.50.34])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby smtp.subspace.kernel.org (Postfix) with ESMTPS id 4B25D39B96A;\n\tTue,  7 Apr 2026 10:09:24 +0000 (UTC)","from\n 2001-1c00-8d85-4b00-266e-96ff-fe07-7dcc.cable.dynamic.v6.ziggo.nl\n ([2001:1c00:8d85:4b00:266e:96ff:fe07:7dcc]\n helo=noisy.programming.kicks-ass.net)\n\tby casper.infradead.org with esmtpsa (Exim 4.98.2 #2 (Red Hat Linux))\n\tid 1wA3N3-00000003PTk-01PM;\n\tTue, 07 Apr 2026 10:09:21 +0000","by noisy.programming.kicks-ass.net (Postfix, from userid 1000)\n\tid 662803005E5; Tue, 07 Apr 2026 12:09:20 +0200 (CEST)"],"ARC-Seal":"i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;\n\tt=1775556565; cv=none;\n b=VA19Swjx87M/d1xzBwhMatQ5simE10bXp4NrS2A5ueTEX/n/TKfW4xb2joHdK7jdvmrOKXrRcG1RePg++wNWp4gKgCOS4V1aPS3qaLrdv5YrOviXYP45zidSt0Px59o+77azAlSCZFcqv0nmQsYl0LWfomopAJH3h8WQN3AX7dI=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=subspace.kernel.org;\n\ts=arc-20240116; t=1775556565; c=relaxed/simple;\n\tbh=cQloyn2Ud+z/jB9mfpqEggLuossDg+Q8/XeUvB75ZcE=;\n\th=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version:\n\t Content-Type:Content-Disposition:In-Reply-To;\n b=VnFGu+wotkNg2Anp4M/WwoHSXawBC1g6GIh9GNoRF70pOd9RG6jwyg6BWN31Fmz22kxSSSPYCrejYgnjVTWdQwA8oHyoAXWgtMsNP40MOQmM0lvNVGloyjRlOvt3milYMidBxz2P1LLHjT56UgVpJ3LlE4OlBxOCvCIhOxzXyNo=","ARC-Authentication-Results":"i=1; smtp.subspace.kernel.org;\n dmarc=pass (p=none dis=none) header.from=infradead.org;\n spf=none smtp.mailfrom=infradead.org;\n dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org\n header.b=dG7LFnnD; arc=none smtp.client-ip=90.155.50.34","DKIM-Signature":"v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed;\n\td=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version:\n\tReferences:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To:\n\tContent-Transfer-Encoding:Content-ID:Content-Description;\n\tbh=Y+pchSuJjEJZKfsWlXfM5v2Cr04XgbOInZ4IMbge9O4=; b=dG7LFnnDS5cqPFNugwTGaC7nJg\n\tb3ANd1WEscfLfkBbBg652O5a2BbKjODvLxHBjqEf/IQ2r1gqwBUDLny8ehDpN2pIFPem5aAg8wC0i\n\tdSNOi5/Pt+J7UJtlXGGMWMbNY3AELZmOdmUpD4dPJjqkq4f6TYuvM1OpJ44z4PXYJ9u6JSzdBtFHz\n\tbIRWne6gADgw3cbL4DiykUjfcWt4SkQkIC49Sv8DKdlJ/90xjMGuhcJiPLQc0ftfQCWUveQe+3LXR\n\tslP3zQDj+yNxg7dxEZCWwbaNkplT67zcgTc15wPLubU4dkder6hGn1tFXXrub8rw9nfpbur2j9z8C\n\tg+GDs8Ug==;","Date":"Tue, 7 Apr 2026 12:09:20 +0200","From":"Peter Zijlstra <peterz@infradead.org>","To":"Thomas Gleixner <tglx@kernel.org>","Cc":"LKML <linux-kernel@vger.kernel.org>,\n\tAlexander Viro <viro@zeniv.linux.org.uk>,\n\tChristian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,\n\tAnna-Maria Behnsen <anna-maria@linutronix.de>,\n\tFrederic Weisbecker <frederic@kernel.org>,\n\tlinux-fsdevel@vger.kernel.org, Calvin Owens <calvin@wbinvd.org>,\n\tIngo Molnar <mingo@kernel.org>, John Stultz <jstultz@google.com>,\n\tStephen Boyd <sboyd@kernel.org>, Sebastian Reichel <sre@kernel.org>,\n\tlinux-pm@vger.kernel.org, Pablo Neira Ayuso <pablo@netfilter.org>,\n\tFlorian Westphal <fw@strlen.de>, Phil Sutter <phil@nwl.cc>,\n\tnetfilter-devel@vger.kernel.org, coreteam@netfilter.org","Subject":"Re: [patch 09/12] fs/timerfd: Use the new alarm/hrtimer functions","Message-ID":"<20260407100920.GT2872@noisy.programming.kicks-ass.net>","References":"<20260407083219.478203185@kernel.org>\n <20260407083248.102440187@kernel.org>","Precedence":"bulk","X-Mailing-List":"netfilter-devel@vger.kernel.org","List-Id":"<netfilter-devel.vger.kernel.org>","List-Subscribe":"<mailto:netfilter-devel+subscribe@vger.kernel.org>","List-Unsubscribe":"<mailto:netfilter-devel+unsubscribe@vger.kernel.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=us-ascii","Content-Disposition":"inline","In-Reply-To":"<20260407083248.102440187@kernel.org>"}},{"id":3674115,"web_url":"http://patchwork.ozlabs.org/comment/3674115/","msgid":"<87bjfv5767.ffs@tglx>","list_archive_url":null,"date":"2026-04-07T11:41:36","subject":"Re: [patch 09/12] fs/timerfd: Use the new alarm/hrtimer functions","submitter":{"id":92397,"url":"http://patchwork.ozlabs.org/api/people/92397/","name":"Thomas Gleixner","email":"tglx@kernel.org"},"content":"On Tue, Apr 07 2026 at 12:09, Peter Zijlstra wrote:\n>> -\t\t\tctx->ticks +=\n>> -\t\t\t\thrtimer_forward_now(&ctx->t.tmr, ctx->tintv)\n>> -\t\t\t\t- 1;\n>\n> (argh!)\n>\n>> -\t\t\thrtimer_restart(&ctx->t.tmr);\n>> -\t\t}\n>> +\t\tctx->ticks += timerfd_restart(ctx);\n>>  \t}\n>>  \tt->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));\n>>  \tt->it_interval = ktime_to_timespec64(ctx->tintv);\n>\n> What's with the -1 thing?\n\nMagic :)\n\nReading the timerfd returns the number of expired ticks since the last\nread or since the timer was armed.\n\nThe expiry callback increments ticks by one, hrtimer_forward_now()\nreturns the number of expired ticks relative to the previous expiry\ntime. So it would double account that.\n\nNot pretty, but we need to increment ticks in the callback because of\nnon-interval timers as for those we don't invoke the forwarding.\n\nThanks,\n\n        tglx","headers":{"Return-Path":"\n <netfilter-devel+bounces-11670-incoming=patchwork.ozlabs.org@vger.kernel.org>","X-Original-To":["incoming@patchwork.ozlabs.org","netfilter-devel@vger.kernel.org"],"Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256\n header.s=k20201202 header.b=peSKzZQp;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org\n (client-ip=2600:3c09:e001:a7::12fc:5321; helo=sto.lore.kernel.org;\n envelope-from=netfilter-devel+bounces-11670-incoming=patchwork.ozlabs.org@vger.kernel.org;\n receiver=patchwork.ozlabs.org)","smtp.subspace.kernel.org;\n\tdkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org\n header.b=\"peSKzZQp\"","smtp.subspace.kernel.org;\n arc=none smtp.client-ip=10.30.226.201"],"Received":["from sto.lore.kernel.org (sto.lore.kernel.org\n [IPv6:2600:3c09:e001:a7::12fc:5321])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4fqkn65F4Jz1xtJ\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 07 Apr 2026 21:41:46 +1000 (AEST)","from smtp.subspace.kernel.org (conduit.subspace.kernel.org\n [100.90.174.1])\n\tby sto.lore.kernel.org (Postfix) with ESMTP id B7B1B3005590\n\tfor <incoming@patchwork.ozlabs.org>; Tue,  7 Apr 2026 11:41:43 +0000 (UTC)","from localhost.localdomain (localhost.localdomain [127.0.0.1])\n\tby smtp.subspace.kernel.org (Postfix) with ESMTP id A9BCB3AF660;\n\tTue,  7 Apr 2026 11:41:40 +0000 (UTC)","from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org\n [10.30.226.201])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby smtp.subspace.kernel.org (Postfix) with ESMTPS id 2DFFE3AE71B;\n\tTue,  7 Apr 2026 11:41:39 +0000 (UTC)","by smtp.kernel.org (Postfix) with ESMTPSA id E475CC19421;\n\tTue,  7 Apr 2026 11:41:38 +0000 (UTC)"],"ARC-Seal":"i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;\n\tt=1775562100; cv=none;\n b=BN+JpfsXq4KZO3F/Z9avjqMIf2pALrZhQIFF3Jlo0BneVjV+HD+fDfiqOi7c/9R+n/hj+fAF636s1QFLckag4ejilsPW/bIAtJI202M24iNsqtH575f8qeHGyMuClbNIaDFy7snylLASZJOWJEdMVNh9L6mRthOqEe+t0IkouEw=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=subspace.kernel.org;\n\ts=arc-20240116; t=1775562100; c=relaxed/simple;\n\tbh=qbshZNj66asvJ47A+aeT0iqHEZRG3IFR7A8nyMw46bE=;\n\th=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID:\n\t MIME-Version:Content-Type;\n b=Ho2wUidSccXkWJ5i3XAEPyWIF0QzbO/zvt1eU24OXONMV5D+WKeC56b9bWTKs5srWUxIwbKvEDsejqoycOlTNjMA0zL0n84nyeOrOqni9qjnEe5kNJ1gtN1WpsjVyH7zShpFnOIjLwPTCxSU9pQnIdPboFmUEWcHjFE8ioUr6Vk=","ARC-Authentication-Results":"i=1; smtp.subspace.kernel.org;\n dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org\n header.b=peSKzZQp; arc=none smtp.client-ip=10.30.226.201","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;\n\ts=k20201202; t=1775562099;\n\tbh=qbshZNj66asvJ47A+aeT0iqHEZRG3IFR7A8nyMw46bE=;\n\th=From:To:Cc:Subject:In-Reply-To:References:Date:From;\n\tb=peSKzZQpzHC1Nzl34iRbgyToOF4HtCapV3RySFz6+MnXnvCq/ijxGDGSTGVL/4MRQ\n\t D/s5jBUey/Pforf1ACh4Rj2doByYaw8fHWiZmSspgZsLCIYQL4IIfXUJjdfnsMIekP\n\t bxEbW0Z4Pe/2A7mAbGJEkH7MjnBjzchbC0DhdloKNUKQcGhm+ziUbjak+Z5MFsRtOP\n\t VuDdx4MYtk26DsHWVSt/V4NM2L/h81LyUM/uJM/M0G1QzSPRrwbPidiZ/6w8wvJ277\n\t BlVlNjc59yPty02ploxjtVsDH/XuyIWCt8tuSjKNiE5p57eMBVFd58EiqFsQ33nQLg\n\t awgN448r0r2nA==","From":"Thomas Gleixner <tglx@kernel.org>","To":"Peter Zijlstra <peterz@infradead.org>","Cc":"LKML <linux-kernel@vger.kernel.org>, Alexander Viro\n <viro@zeniv.linux.org.uk>, Christian Brauner <brauner@kernel.org>, Jan\n Kara <jack@suse.cz>, Anna-Maria Behnsen <anna-maria@linutronix.de>,\n Frederic Weisbecker <frederic@kernel.org>, linux-fsdevel@vger.kernel.org,\n Calvin Owens <calvin@wbinvd.org>, Ingo Molnar <mingo@kernel.org>, John\n Stultz <jstultz@google.com>, Stephen Boyd <sboyd@kernel.org>, Sebastian\n Reichel <sre@kernel.org>, linux-pm@vger.kernel.org, Pablo Neira Ayuso\n <pablo@netfilter.org>, Florian Westphal <fw@strlen.de>, Phil Sutter\n <phil@nwl.cc>, netfilter-devel@vger.kernel.org, coreteam@netfilter.org","Subject":"Re: [patch 09/12] fs/timerfd: Use the new alarm/hrtimer functions","In-Reply-To":"<20260407100920.GT2872@noisy.programming.kicks-ass.net>","References":"<20260407083219.478203185@kernel.org>\n <20260407083248.102440187@kernel.org>\n <20260407100920.GT2872@noisy.programming.kicks-ass.net>","Date":"Tue, 07 Apr 2026 13:41:36 +0200","Message-ID":"<87bjfv5767.ffs@tglx>","Precedence":"bulk","X-Mailing-List":"netfilter-devel@vger.kernel.org","List-Id":"<netfilter-devel.vger.kernel.org>","List-Subscribe":"<mailto:netfilter-devel+subscribe@vger.kernel.org>","List-Unsubscribe":"<mailto:netfilter-devel+unsubscribe@vger.kernel.org>","MIME-Version":"1.0","Content-Type":"text/plain"}}]