mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Guillaume Knispel <gknispel@proformatique.com>
To: Andrew Morton <akpm@osdl.org>
Cc: Ulrich Drepper <drepper@redhat.com>, Ingo Molnar <mingo@elte.hu>,
	Thomas Gleixner <tglx@linutronix.de>,
	Rusty Russell <rusty@rustcorp.com.au>,
	LKML <linux-kernel@vger.kernel.org>
Subject: [PATCH] correct the behavior of printk_timed_ratelimit()
Date: Tue, 17 Mar 2009 16:18:42 +0100	[thread overview]
Message-ID: <20090317161842.0059096b@xilun.lan.proformatique.com> (raw)

Hi,

The behavior provided by printk_timed_ratelimit() is, in some
situations, probably not what a caller would reasonably expect:

bool printk_timed_ratelimit(unsigned long *caller_jiffies,
			unsigned int interval_msecs)
{
	if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
		*caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
		return true;
	}
	return false;
}

On a 32 bit computer, if printk_timed_ratelimit() is initially called at
time jiffies == Ja, *caller_jiffies is set to
Ja + msecs_to_jiffies(interval_msecs): let's say Ja + 42 for this
example.  If this caller then don't call printk_timed_ratelimit() until
jiffies == Ja + (1 << 31) + 42 (which can happen as soon as ~ 25 days
later on a 1000 HZ system), printk_timed_ratelimit() will then always
return false to this caller until jiffies loops completely (1 << 31 more
ticks).

The proposed patch makes it only return false if jiffies is in the small
time window starting at the previous call when true was returned and
ending interval_msecs later.  Note that if jiffies loops completely
between two calls to printk_timed_ratelimit(), it will obviously still
wrongly return false, but this is something with a low probability.  If
something completely reliable is needed I guess jiffies_64 must be used
(which the proposed patch does not do).

Cheers,
Guillaume Knispel




Avoid potential situations where printk_timed_ratelimit() can return
false for no good reason during up to (1 << 31) / HZ seconds on 32 bits
computers.  It still can wrongly return false for a duration of up to
interval_msecs if jiffies has been incremented (k * (1 << 32) + e) times
-- with e < msecs_to_jiffies(interval_msecs) -- since the last call
which returned true.

Note that *caller_jiffies now stores jiffies just before the function
returns true, instead of jiffies + msecs_to_jiffies(interval_msecs)
previously.  Also note, if interval_msecs is not constant for a given
caller, that the interval_msecs used is now the one passed for the
current call instead of previously the one used for the last call which
returned true.

Signed-off-by: Guillaume Knispel <gknispel@proformatique.com>

---
diff --git a/kernel/printk.c b/kernel/printk.c
index e3602d0..2be7199 100644
--- a/kernel/printk.c
+++ b/kernel/printk.c
@@ -1292,8 +1292,11 @@ EXPORT_SYMBOL(printk_ratelimit);
 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
 			unsigned int interval_msecs)
 {
-	if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
-		*caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
+	if (*caller_jiffies == 0
+			|| !time_in_range(jiffies, *caller_jiffies,
+					*caller_jiffies
+					+ msecs_to_jiffies(interval_msecs))) {
+		*caller_jiffies = jiffies;
 		return true;
 	}
 	return false;

             reply	other threads:[~2009-03-17 15:20 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-17 15:18 Guillaume Knispel [this message]
2009-03-17 16:03 ` [tip:core/printk] printk: " Guillaume Knispel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090317161842.0059096b@xilun.lan.proformatique.com \
    --to=gknispel@proformatique.com \
    --cc=akpm@osdl.org \
    --cc=drepper@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=rusty@rustcorp.com.au \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®